2016-05-12 23:43:17 +03:00
|
|
|
/*
|
2017-01-21 09:02:33 +03:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2017-05-02 04:45:31 +03:00
|
|
|
#include "eden/fs/inodes/FileInode.h"
|
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
#include <folly/FileUtil.h>
|
|
|
|
#include <folly/experimental/logging/xlog.h>
|
|
|
|
#include <folly/io/Cursor.h>
|
|
|
|
#include <folly/io/IOBuf.h>
|
2017-09-19 20:59:46 +03:00
|
|
|
#include <folly/io/async/EventBase.h>
|
2017-07-27 09:39:02 +03:00
|
|
|
#include <openssl/sha.h>
|
2017-05-02 04:45:31 +03:00
|
|
|
#include "eden/fs/inodes/EdenMount.h"
|
|
|
|
#include "eden/fs/inodes/FileHandle.h"
|
|
|
|
#include "eden/fs/inodes/InodeError.h"
|
|
|
|
#include "eden/fs/inodes/Overlay.h"
|
|
|
|
#include "eden/fs/inodes/TreeInode.h"
|
2016-06-09 04:59:51 +03:00
|
|
|
#include "eden/fs/model/Blob.h"
|
2016-05-28 04:16:30 +03:00
|
|
|
#include "eden/fs/model/Hash.h"
|
2017-05-02 04:45:31 +03:00
|
|
|
#include "eden/fs/store/BlobMetadata.h"
|
2016-06-09 04:59:51 +03:00
|
|
|
#include "eden/fs/store/ObjectStore.h"
|
2017-07-27 09:39:02 +03:00
|
|
|
#include "eden/fs/utils/Bug.h"
|
2017-12-05 20:55:31 +03:00
|
|
|
#include "eden/fs/utils/Clock.h"
|
2017-12-15 03:36:38 +03:00
|
|
|
#include "eden/fs/utils/DirType.h"
|
2017-12-12 23:23:57 +03:00
|
|
|
#include "eden/fs/utils/UnboundedQueueThreadPool.h"
|
2017-04-14 21:31:48 +03:00
|
|
|
#include "eden/fs/utils/XAttr.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2017-11-04 01:58:04 +03:00
|
|
|
using folly::ByteRange;
|
2018-01-26 22:17:36 +03:00
|
|
|
using folly::checkUnixError;
|
2016-05-12 23:43:17 +03:00
|
|
|
using folly::Future;
|
2018-01-26 22:17:36 +03:00
|
|
|
using folly::makeFuture;
|
2016-05-12 23:43:17 +03:00
|
|
|
using folly::StringPiece;
|
2017-07-27 09:39:02 +03:00
|
|
|
using folly::Unit;
|
2017-03-11 05:28:13 +03:00
|
|
|
using std::shared_ptr;
|
2016-05-12 23:43:17 +03:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2017-03-11 05:28:13 +03:00
|
|
|
FileInode::State::State(
|
|
|
|
FileInode* inode,
|
|
|
|
mode_t m,
|
2017-07-27 21:48:19 +03:00
|
|
|
const folly::Optional<Hash>& h,
|
|
|
|
const timespec& lastCheckoutTime)
|
2017-08-15 09:07:54 +03:00
|
|
|
: mode(m), hash(h) {
|
2017-11-21 20:14:31 +03:00
|
|
|
if (!hash.hasValue()) {
|
|
|
|
// File is materialized; read out the timestamps but don't keep it open.
|
2017-07-17 23:17:42 +03:00
|
|
|
auto filePath = inode->getLocalPath();
|
2017-11-21 20:14:31 +03:00
|
|
|
(void)Overlay::openFile(
|
2017-08-11 21:34:52 +03:00
|
|
|
filePath.c_str(), Overlay::kHeaderIdentifierFile, timeStamps);
|
2017-11-20 23:03:28 +03:00
|
|
|
tag = MATERIALIZED_IN_OVERLAY;
|
2017-08-05 06:14:18 +03:00
|
|
|
} else {
|
2018-02-01 23:21:03 +03:00
|
|
|
timeStamps.setAll(lastCheckoutTime);
|
2017-11-20 23:03:28 +03:00
|
|
|
tag = NOT_LOADED;
|
2017-07-17 23:17:42 +03:00
|
|
|
}
|
2017-11-08 03:07:16 +03:00
|
|
|
|
|
|
|
checkInvariants();
|
2017-07-17 23:17:42 +03:00
|
|
|
}
|
2017-03-11 05:28:13 +03:00
|
|
|
|
2017-03-31 09:38:42 +03:00
|
|
|
FileInode::State::State(
|
|
|
|
FileInode* inode,
|
|
|
|
mode_t m,
|
2018-01-25 00:17:27 +03:00
|
|
|
const timespec& creationTime)
|
|
|
|
: tag(MATERIALIZED_IN_OVERLAY), mode(m) {
|
2018-02-01 23:21:03 +03:00
|
|
|
timeStamps.setAll(creationTime);
|
2017-11-08 03:07:16 +03:00
|
|
|
checkInvariants();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileInode::State::State::checkInvariants() {
|
2017-11-20 23:03:28 +03:00
|
|
|
switch (tag) {
|
|
|
|
case NOT_LOADED:
|
|
|
|
CHECK(hash);
|
|
|
|
CHECK(!blobLoadingPromise);
|
|
|
|
CHECK(!blob);
|
|
|
|
CHECK(!file);
|
|
|
|
CHECK(!sha1Valid);
|
|
|
|
return;
|
|
|
|
case BLOB_LOADING:
|
|
|
|
CHECK(hash);
|
|
|
|
CHECK(blobLoadingPromise);
|
|
|
|
CHECK(!blob);
|
|
|
|
CHECK(!file);
|
|
|
|
CHECK(!sha1Valid);
|
|
|
|
return;
|
|
|
|
case BLOB_LOADED:
|
|
|
|
CHECK(hash);
|
|
|
|
CHECK(!blobLoadingPromise);
|
|
|
|
CHECK(blob);
|
|
|
|
CHECK(!file);
|
|
|
|
CHECK(!sha1Valid);
|
|
|
|
DCHECK_EQ(blob->getHash(), hash.value());
|
|
|
|
return;
|
|
|
|
case MATERIALIZED_IN_OVERLAY:
|
|
|
|
// 'materialized'
|
|
|
|
CHECK(!hash);
|
|
|
|
CHECK(!blobLoadingPromise);
|
|
|
|
CHECK(!blob);
|
2018-01-04 04:18:30 +03:00
|
|
|
if (file) {
|
|
|
|
CHECK_GT(openCount, 0);
|
|
|
|
}
|
2017-11-21 20:14:31 +03:00
|
|
|
if (openCount == 0) {
|
|
|
|
// file is lazily set, so the only interesting assertion is
|
|
|
|
// that it's not open if openCount is zero.
|
|
|
|
CHECK(!file);
|
|
|
|
}
|
2017-11-20 23:03:28 +03:00
|
|
|
return;
|
2017-11-08 03:07:16 +03:00
|
|
|
}
|
2017-11-20 23:03:28 +03:00
|
|
|
|
|
|
|
XLOG(FATAL) << "Unexpected tag value: " << tag;
|
2017-07-27 21:48:19 +03:00
|
|
|
}
|
2017-11-20 23:03:28 +03:00
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
void FileInode::State::closeFile() {
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
folly::File FileInode::getFile(FileInode::State& state) const {
|
|
|
|
DCHECK(state.isMaterialized())
|
|
|
|
<< "must only be called for materialized files";
|
|
|
|
|
|
|
|
if (state.openCount > 0 && !state.isFileOpen()) {
|
|
|
|
// When opening a file handle to the file, the openCount is incremented but
|
|
|
|
// the overlay file is not actually opened. Instead, it's opened lazily
|
|
|
|
// here.
|
|
|
|
state.file = folly::File(getLocalPath().c_str(), O_RDWR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.isFileOpen()) {
|
|
|
|
// Return a non-owning copy of the file object that we already have
|
|
|
|
return folly::File(state.file.fd(), /*ownsFd=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have and shouldn't keep a file around, so we return
|
|
|
|
// a File temporary instead.
|
|
|
|
return folly::File(getLocalPath().c_str(), O_RDWR);
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:17:42 +03:00
|
|
|
/*
|
|
|
|
* Defined State Destructor explicitly to avoid including
|
|
|
|
* some header files in FileInode.h
|
|
|
|
*/
|
|
|
|
FileInode::State::~State() = default;
|
2017-03-11 05:28:13 +03:00
|
|
|
|
2018-01-04 04:18:26 +03:00
|
|
|
std::tuple<FileInodePtr, FileInode::FileHandlePtr> FileInode::create(
|
2018-01-03 03:25:03 +03:00
|
|
|
fusell::InodeNumber ino,
|
2017-11-16 11:09:32 +03:00
|
|
|
TreeInodePtr parentInode,
|
|
|
|
PathComponentPiece name,
|
|
|
|
mode_t mode,
|
|
|
|
folly::File&& file,
|
2018-01-25 00:17:27 +03:00
|
|
|
timespec ctime) {
|
2017-11-20 23:03:28 +03:00
|
|
|
// The FileInode is in MATERIALIZED_IN_OVERLAY state.
|
2017-11-16 11:09:32 +03:00
|
|
|
auto inode = FileInodePtr::makeNew(
|
2018-01-25 00:17:27 +03:00
|
|
|
ino, parentInode, name, mode, std::move(file), ctime);
|
2017-11-16 11:09:32 +03:00
|
|
|
|
2018-01-04 04:18:26 +03:00
|
|
|
return inode->state_.withWLock([&](auto& state) {
|
|
|
|
auto fileHandle = std::make_shared<FileHandle>(
|
|
|
|
inode, [&state] { fileHandleDidOpen(state); });
|
|
|
|
state.file = std::move(file);
|
|
|
|
DCHECK_EQ(state.openCount, 1)
|
|
|
|
<< "open count cannot be anything other than 1";
|
|
|
|
return std::make_tuple(inode, fileHandle);
|
|
|
|
});
|
2017-11-16 11:09:32 +03:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
// The FileInode is in NOT_LOADED or MATERIALIZED_IN_OVERLAY state.
|
2016-12-01 02:48:04 +03:00
|
|
|
FileInode::FileInode(
|
2018-01-03 03:25:03 +03:00
|
|
|
fusell::InodeNumber ino,
|
2016-12-13 04:48:45 +03:00
|
|
|
TreeInodePtr parentInode,
|
2016-12-13 04:48:43 +03:00
|
|
|
PathComponentPiece name,
|
2017-03-11 05:28:13 +03:00
|
|
|
mode_t mode,
|
|
|
|
const folly::Optional<Hash>& hash)
|
2017-12-15 03:36:38 +03:00
|
|
|
: InodeBase(ino, mode_to_dtype(mode), std::move(parentInode), name),
|
2017-07-27 21:48:19 +03:00
|
|
|
state_(
|
|
|
|
folly::in_place,
|
|
|
|
this,
|
|
|
|
mode,
|
|
|
|
hash,
|
|
|
|
getMount()->getLastCheckoutTime()) {}
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
// The FileInode is in MATERIALIZED_IN_OVERLAY state.
|
2016-12-01 02:48:04 +03:00
|
|
|
FileInode::FileInode(
|
2018-01-03 03:25:03 +03:00
|
|
|
fusell::InodeNumber ino,
|
2016-12-13 04:48:45 +03:00
|
|
|
TreeInodePtr parentInode,
|
2016-12-13 04:48:43 +03:00
|
|
|
PathComponentPiece name,
|
2017-03-11 05:28:13 +03:00
|
|
|
mode_t mode,
|
2017-03-31 09:38:42 +03:00
|
|
|
folly::File&& file,
|
2018-01-25 00:17:27 +03:00
|
|
|
timespec ctime)
|
2017-12-15 03:36:38 +03:00
|
|
|
: InodeBase(ino, mode_to_dtype(mode), std::move(parentInode), name),
|
2018-01-25 00:17:27 +03:00
|
|
|
state_(folly::in_place, this, mode, ctime) {}
|
2016-07-06 05:53:15 +03:00
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
folly::Future<fusell::Dispatcher::Attr> FileInode::getattr() {
|
2016-05-17 00:48:30 +03:00
|
|
|
// Future optimization opportunity: right now, if we have not already
|
2017-03-11 05:28:13 +03:00
|
|
|
// materialized the data from the entry, we have to materialize it
|
2016-05-17 00:48:30 +03:00
|
|
|
// from the store. If we augmented our metadata we could avoid this,
|
|
|
|
// and this would speed up operations like `ls`.
|
2017-11-04 04:54:57 +03:00
|
|
|
return stat().then(
|
2018-02-09 06:32:04 +03:00
|
|
|
[](const struct stat& st) { return fusell::Dispatcher::Attr{st}; });
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2017-08-14 23:34:14 +03:00
|
|
|
folly::Future<fusell::Dispatcher::Attr> FileInode::setInodeAttr(
|
2018-01-03 03:25:03 +03:00
|
|
|
const fuse_setattr_in& attr) {
|
2018-01-08 22:48:53 +03:00
|
|
|
// Minor optimization: if we know that the file is being completely truncated
|
2016-07-02 01:08:47 +03:00
|
|
|
// as part of this operation, there's no need to fetch the underlying data,
|
|
|
|
// so pass on the truncate flag our underlying open call
|
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
bool truncate = (attr.valid & FATTR_SIZE) && attr.size == 0;
|
2017-12-05 02:07:44 +03:00
|
|
|
auto future = truncate ? (materializeAndTruncate(), makeFuture())
|
|
|
|
: materializeForWrite();
|
2018-01-03 03:25:03 +03:00
|
|
|
return future.then([self = inodePtrFromThis(), attr]() {
|
2017-12-05 02:07:44 +03:00
|
|
|
self->materializeInParent();
|
2017-08-05 06:14:19 +03:00
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
auto result = fusell::Dispatcher::Attr{self->getMount()->initStatData()};
|
2017-08-05 06:14:19 +03:00
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
auto state = self->state_.wlock();
|
|
|
|
CHECK_EQ(State::MATERIALIZED_IN_OVERLAY, state->tag)
|
|
|
|
<< "Must have a file in the overlay at this point";
|
|
|
|
auto file = self->getFile(*state);
|
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
// Set the size of the file when FATTR_SIZE is set
|
|
|
|
if (attr.valid & FATTR_SIZE) {
|
|
|
|
checkUnixError(ftruncate(file.fd(), attr.size + Overlay::kHeaderLength));
|
2017-12-05 02:07:44 +03:00
|
|
|
}
|
2017-08-05 06:14:19 +03:00
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
if (attr.valid & FATTR_MODE) {
|
2017-12-05 02:07:44 +03:00
|
|
|
// The mode data is stored only in inode_->state_.
|
|
|
|
// (We don't set mode bits on the overlay file as that may incorrectly
|
|
|
|
// prevent us from reading or writing the overlay data).
|
|
|
|
// Make sure we preserve the file type bits, and only update
|
|
|
|
// permissions.
|
2018-01-03 03:25:03 +03:00
|
|
|
state->mode = (state->mode & S_IFMT) | (07777 & attr.mode);
|
2017-12-05 02:07:44 +03:00
|
|
|
}
|
2017-08-05 06:14:19 +03:00
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
// Set in-memory timeStamps
|
2018-02-01 23:21:03 +03:00
|
|
|
state->timeStamps.setattrTimes(self->getClock(), attr);
|
2017-12-05 02:07:44 +03:00
|
|
|
|
|
|
|
// We need to call fstat function here to get the size of the overlay
|
|
|
|
// file. We might update size in the result while truncating the file
|
2018-01-03 03:25:03 +03:00
|
|
|
// when FATTR_SIZE flag is set but when the flag is not set we
|
2017-12-05 02:07:44 +03:00
|
|
|
// have to return the correct size of the file even if some size is sent
|
|
|
|
// in attr.st.st_size.
|
2018-02-09 06:32:04 +03:00
|
|
|
struct stat overlayStat;
|
|
|
|
checkUnixError(fstat(file.fd(), &overlayStat));
|
2017-12-05 02:07:44 +03:00
|
|
|
result.st.st_ino = self->getNodeId();
|
2018-02-09 06:32:04 +03:00
|
|
|
result.st.st_size = overlayStat.st_size - Overlay::kHeaderLength;
|
2017-12-05 02:07:44 +03:00
|
|
|
result.st.st_atim = state->timeStamps.atime;
|
|
|
|
result.st.st_ctim = state->timeStamps.ctime;
|
|
|
|
result.st.st_mtim = state->timeStamps.mtime;
|
|
|
|
result.st.st_mode = state->mode;
|
2018-02-09 06:32:04 +03:00
|
|
|
result.st.st_nlink = 1;
|
|
|
|
updateBlockCount(result.st);
|
2016-09-19 22:48:11 +03:00
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
state->checkInvariants();
|
2017-11-08 03:07:16 +03:00
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
// Update the Journal
|
|
|
|
self->updateJournal();
|
|
|
|
return result;
|
|
|
|
});
|
2016-07-02 01:08:47 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
folly::Future<std::string> FileInode::readlink() {
|
2017-03-02 19:16:18 +03:00
|
|
|
{
|
2017-11-08 03:07:16 +03:00
|
|
|
// TODO: Since the type component of the mode is immutable, it could be
|
|
|
|
// moved out of the locked state, obviating the need to acquire a lock
|
|
|
|
// here.
|
|
|
|
auto state = state_.rlock();
|
2017-03-11 05:28:13 +03:00
|
|
|
if (!S_ISLNK(state->mode)) {
|
2017-03-02 19:16:18 +03:00
|
|
|
// man 2 readlink says: EINVAL The named file is not a symbolic link.
|
|
|
|
throw InodeError(EINVAL, inodePtrFromThis(), "not a symlink");
|
|
|
|
}
|
2016-05-17 00:48:28 +03:00
|
|
|
}
|
|
|
|
|
2017-03-02 19:16:18 +03:00
|
|
|
// The symlink contents are simply the file contents!
|
2017-11-04 03:46:03 +03:00
|
|
|
return readAll();
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:18:26 +03:00
|
|
|
void FileInode::fileHandleDidOpen(State& state) {
|
2017-11-21 20:14:31 +03:00
|
|
|
// Don't immediately open the file when transitioning from 0 to 1. Open it
|
|
|
|
// when getFile() is called.
|
2018-01-04 04:18:26 +03:00
|
|
|
state.openCount += 1;
|
2017-11-21 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
void FileInode::fileHandleDidClose() {
|
2017-11-21 20:14:31 +03:00
|
|
|
auto state = state_.wlock();
|
|
|
|
DCHECK_GT(state->openCount, 0);
|
|
|
|
if (--state->openCount == 0) {
|
2018-01-04 04:18:30 +03:00
|
|
|
switch (state->tag) {
|
|
|
|
case State::BLOB_LOADED:
|
|
|
|
state->blob.reset();
|
|
|
|
state->tag = State::NOT_LOADED;
|
|
|
|
break;
|
|
|
|
case State::MATERIALIZED_IN_OVERLAY:
|
|
|
|
// TODO: Before closing the file handle, it might make sense to write
|
|
|
|
// in-memory timestamps into the overlay, even if the inode remains in
|
|
|
|
// memory. This would ensure timestamps persist even if the edenfs
|
|
|
|
// process crashes or otherwise exits without unloading all inodes.
|
|
|
|
state->closeFile();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 03:07:16 +03:00
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
AbsolutePath FileInode::getLocalPath() const {
|
2017-02-11 01:16:00 +03:00
|
|
|
return getMount()->getOverlay()->getFilePath(getNodeId());
|
2016-05-17 00:48:28 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
folly::Optional<bool> FileInode::isSameAsFast(const Hash& blobID, mode_t mode) {
|
2017-02-16 07:31:48 +03:00
|
|
|
// When comparing mode bits, we only care about the
|
|
|
|
// file type and owner permissions.
|
|
|
|
auto relevantModeBits = [](mode_t m) { return (m & (S_IFMT | S_IRWXU)); };
|
|
|
|
|
2017-11-08 03:07:16 +03:00
|
|
|
auto state = state_.rlock();
|
2017-05-02 04:45:31 +03:00
|
|
|
if (relevantModeBits(state->mode) != relevantModeBits(mode)) {
|
2017-07-27 09:39:02 +03:00
|
|
|
return false;
|
2017-05-02 04:45:31 +03:00
|
|
|
}
|
2017-03-11 05:28:13 +03:00
|
|
|
|
2017-05-02 04:45:31 +03:00
|
|
|
if (state->hash.hasValue()) {
|
|
|
|
// This file is not materialized, so we can just compare hashes
|
2017-07-27 09:39:02 +03:00
|
|
|
return state->hash.value() == blobID;
|
2017-05-02 04:45:31 +03:00
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
return folly::none;
|
2017-05-02 04:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileInode::isSameAs(const Blob& blob, mode_t mode) {
|
|
|
|
auto result = isSameAsFast(blob.getHash(), mode);
|
2017-07-27 09:39:02 +03:00
|
|
|
if (result.hasValue()) {
|
|
|
|
return result.value();
|
2017-05-02 04:45:31 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 02:51:44 +03:00
|
|
|
return getSha1().value() == Hash::sha1(&blob.getContents());
|
2017-05-02 04:45:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
folly::Future<bool> FileInode::isSameAs(const Hash& blobID, mode_t mode) {
|
|
|
|
auto result = isSameAsFast(blobID, mode);
|
2017-07-27 09:39:02 +03:00
|
|
|
if (result.hasValue()) {
|
|
|
|
return makeFuture(result.value());
|
2017-02-16 07:31:48 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:58:04 +03:00
|
|
|
return getMount()->getObjectStore()->getBlobMetadata(blobID).then(
|
|
|
|
[self = inodePtrFromThis()](const BlobMetadata& metadata) {
|
2017-11-10 02:51:44 +03:00
|
|
|
return self->getSha1().value() == metadata.sha1;
|
2017-05-02 04:45:31 +03:00
|
|
|
});
|
2017-02-16 07:31:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:20:34 +03:00
|
|
|
mode_t FileInode::getMode() const {
|
2017-03-11 05:28:13 +03:00
|
|
|
return state_.rlock()->mode;
|
2017-03-03 01:20:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mode_t FileInode::getPermissions() const {
|
|
|
|
return (getMode() & 07777);
|
|
|
|
}
|
|
|
|
|
2017-03-11 05:28:13 +03:00
|
|
|
folly::Optional<Hash> FileInode::getBlobHash() const {
|
|
|
|
return state_.rlock()->hash;
|
|
|
|
}
|
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
folly::Future<std::shared_ptr<fusell::FileHandle>> FileInode::open(int flags) {
|
2017-03-02 19:16:18 +03:00
|
|
|
{
|
2017-11-08 03:07:16 +03:00
|
|
|
// TODO: Since the type component of the mode is immutable, it could be
|
|
|
|
// moved out of the locked state, obviating the need to acquire a lock
|
|
|
|
// here.
|
|
|
|
auto state = state_.rlock();
|
2017-03-02 19:16:18 +03:00
|
|
|
|
2017-03-11 05:28:13 +03:00
|
|
|
if (S_ISLNK(state->mode)) {
|
2017-03-02 19:16:18 +03:00
|
|
|
// Linux reports ELOOP if you try to open a symlink with O_NOFOLLOW set.
|
|
|
|
// Since it isn't clear whether FUSE will allow this to happen, this
|
|
|
|
// is a speculative defense against that happening; the O_PATH flag
|
|
|
|
// does allow a file handle to be opened on a symlink on Linux,
|
|
|
|
// but does not allow it to be used for real IO operations. We're
|
|
|
|
// punting on handling those situations here for now.
|
|
|
|
throw InodeError(ELOOP, inodePtrFromThis(), "is a symlink");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:18:26 +03:00
|
|
|
auto fileHandle = state_.withWLock([&](auto& state) {
|
|
|
|
// Creating the FileHandle increments openCount, which causes the truncation
|
|
|
|
// and materialization paths to cache the overlay's file handle in the
|
|
|
|
// state.
|
|
|
|
return std::make_shared<FileHandle>(
|
|
|
|
inodePtrFromThis(), [&state] { fileHandleDidOpen(state); });
|
|
|
|
});
|
2017-12-05 02:07:44 +03:00
|
|
|
|
2018-01-03 03:25:03 +03:00
|
|
|
if (flags & O_TRUNC) {
|
2017-12-05 02:07:44 +03:00
|
|
|
materializeAndTruncate();
|
2018-01-03 03:25:03 +03:00
|
|
|
} else if (flags & (O_RDWR | O_WRONLY | O_CREAT)) {
|
2017-12-05 22:08:56 +03:00
|
|
|
// Begin materializing the data into the overlay, but return the FileHandle
|
|
|
|
// immediately.
|
|
|
|
(void)materializeForWrite();
|
2016-09-10 02:56:00 +03:00
|
|
|
} else {
|
2017-12-05 02:07:44 +03:00
|
|
|
// Begin prefetching the data as it's likely to be needed soon.
|
|
|
|
(void)ensureDataLoaded();
|
2016-09-10 02:56:00 +03:00
|
|
|
}
|
2017-12-05 22:08:56 +03:00
|
|
|
|
|
|
|
return fileHandle;
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2017-03-11 05:28:08 +03:00
|
|
|
void FileInode::materializeInParent() {
|
|
|
|
auto renameLock = getMount()->acquireRenameLock();
|
|
|
|
auto loc = getLocationInfo(renameLock);
|
|
|
|
if (loc.parent && !loc.unlinked) {
|
|
|
|
loc.parent->childMaterialized(renameLock, loc.name, getNodeId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
Future<vector<string>> FileInode::listxattr() {
|
2016-05-12 23:43:17 +03:00
|
|
|
// Currently, we only return a non-empty vector for regular files, and we
|
|
|
|
// assume that the SHA-1 is present without checking the ObjectStore.
|
|
|
|
vector<string> attributes;
|
|
|
|
|
2017-03-11 05:28:13 +03:00
|
|
|
{
|
|
|
|
auto state = state_.rlock();
|
|
|
|
if (S_ISREG(state->mode)) {
|
|
|
|
attributes.emplace_back(kXattrSha1.str());
|
|
|
|
}
|
2016-09-10 02:56:00 +03:00
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
2016-12-01 02:48:04 +03:00
|
|
|
Future<string> FileInode::getxattr(StringPiece name) {
|
2016-05-12 23:43:17 +03:00
|
|
|
// Currently, we only support the xattr for the SHA-1 of a regular file.
|
2016-05-26 18:22:22 +03:00
|
|
|
if (name != kXattrSha1) {
|
2017-03-11 05:28:13 +03:00
|
|
|
return makeFuture<string>(InodeError(kENOATTR, inodePtrFromThis()));
|
2016-05-26 18:22:22 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 02:51:44 +03:00
|
|
|
return getSha1().then([](Hash hash) { return hash.toString(); });
|
2016-05-28 04:16:30 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:06:52 +03:00
|
|
|
Future<Hash> FileInode::getSha1() {
|
2017-07-27 09:39:02 +03:00
|
|
|
auto state = state_.wlock();
|
2017-11-08 03:07:16 +03:00
|
|
|
state->checkInvariants();
|
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
switch (state->tag) {
|
|
|
|
case State::NOT_LOADED:
|
|
|
|
case State::BLOB_LOADING:
|
|
|
|
case State::BLOB_LOADED:
|
|
|
|
// If a file is not materialized it should have a hash value.
|
|
|
|
return getObjectStore()
|
|
|
|
->getBlobMetadata(state->hash.value())
|
|
|
|
.then([](const BlobMetadata& metadata) { return metadata.sha1; });
|
|
|
|
case State::MATERIALIZED_IN_OVERLAY:
|
2017-11-21 20:14:31 +03:00
|
|
|
auto file = getFile(*state);
|
2017-11-20 23:03:28 +03:00
|
|
|
if (state->sha1Valid) {
|
2017-11-21 20:14:31 +03:00
|
|
|
auto shaStr = fgetxattr(file.fd(), kXattrSha1);
|
2017-11-20 23:03:28 +03:00
|
|
|
if (!shaStr.empty()) {
|
|
|
|
return Hash(shaStr);
|
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
2017-11-21 20:14:31 +03:00
|
|
|
return recomputeAndStoreSha1(state, file);
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
2017-11-20 23:03:28 +03:00
|
|
|
|
|
|
|
XLOG(FATAL) << "FileInode in illegal state: " << state->tag;
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 04:54:57 +03:00
|
|
|
folly::Future<struct stat> FileInode::stat() {
|
|
|
|
return ensureDataLoaded().then([self = inodePtrFromThis()]() {
|
|
|
|
auto st = self->getMount()->initStatData();
|
|
|
|
st.st_nlink = 1;
|
2018-02-09 06:32:04 +03:00
|
|
|
st.st_ino = self->getNodeId();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
auto state = self->state_.wlock();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
if (state->tag == State::MATERIALIZED_IN_OVERLAY) {
|
2017-11-21 20:14:31 +03:00
|
|
|
auto file = self->getFile(*state);
|
2017-11-04 04:54:57 +03:00
|
|
|
// We are calling fstat only to get the size of the file.
|
2018-02-09 06:32:04 +03:00
|
|
|
struct stat overlayStat;
|
|
|
|
checkUnixError(fstat(file.fd(), &overlayStat));
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2018-02-09 06:32:04 +03:00
|
|
|
if (overlayStat.st_size < Overlay::kHeaderLength) {
|
2017-11-04 04:54:57 +03:00
|
|
|
auto filePath = self->getLocalPath();
|
|
|
|
EDEN_BUG() << "Overlay file " << filePath
|
2018-02-09 06:32:04 +03:00
|
|
|
<< " is too short for header: size=" << overlayStat.st_size;
|
2017-11-04 04:54:57 +03:00
|
|
|
}
|
2018-02-09 06:32:04 +03:00
|
|
|
st.st_size = overlayStat.st_size - Overlay::kHeaderLength;
|
2017-11-04 04:54:57 +03:00
|
|
|
} else {
|
2018-01-04 04:18:30 +03:00
|
|
|
// blob is guaranteed set because ensureDataLoaded() returns a FileHandle
|
|
|
|
// so openCount > 0.
|
2017-11-04 04:54:57 +03:00
|
|
|
CHECK(state->blob);
|
|
|
|
auto buf = state->blob->getContents();
|
|
|
|
st.st_size = buf.computeChainDataLength();
|
|
|
|
|
|
|
|
// NOTE: we don't set rdev to anything special here because we
|
|
|
|
// don't support committing special device nodes.
|
2017-03-11 05:28:13 +03:00
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
|
|
|
|
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
|
2017-11-04 04:54:57 +03:00
|
|
|
st.st_atim = state->timeStamps.atime;
|
|
|
|
st.st_ctim = state->timeStamps.ctime;
|
|
|
|
st.st_mtim = state->timeStamps.mtime;
|
2017-08-15 09:07:53 +03:00
|
|
|
#else
|
2017-11-04 04:54:57 +03:00
|
|
|
st.st_atime = state->timeStamps.atime.tv_sec;
|
|
|
|
st.st_mtime = state->timeStamps.mtime.tv_sec;
|
|
|
|
st.st_ctime = state->timeStamps.ctime.tv_sec;
|
2017-07-27 09:39:02 +03:00
|
|
|
#endif
|
2017-11-04 04:54:57 +03:00
|
|
|
st.st_mode = state->mode;
|
2018-02-09 06:32:04 +03:00
|
|
|
updateBlockCount(st);
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-04 04:54:57 +03:00
|
|
|
return st;
|
|
|
|
});
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:32:04 +03:00
|
|
|
void FileInode::updateBlockCount(struct stat& st) {
|
|
|
|
// Compute a value to store in st_blocks based on st_size.
|
|
|
|
// Note that st_blocks always refers to 512 byte blocks, regardless of the
|
|
|
|
// value we report in st.st_blksize.
|
|
|
|
static constexpr off_t kBlockSize = 512;
|
|
|
|
st.st_blocks = ((st.st_size + kBlockSize - 1) / kBlockSize);
|
|
|
|
}
|
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
void FileInode::flush(uint64_t /* lock_owner */) {
|
2017-12-05 22:08:56 +03:00
|
|
|
// This is called by FUSE when a file handle is closed.
|
|
|
|
// https://github.com/libfuse/libfuse/wiki/FAQ#which-method-is-called-on-the-close-system-call
|
2017-07-27 09:39:02 +03:00
|
|
|
// We have no write buffers, so there is nothing for us to flush,
|
|
|
|
// but let's take this opportunity to update the sha1 attribute.
|
|
|
|
auto state = state_.wlock();
|
2017-11-21 20:14:31 +03:00
|
|
|
if (state->isFileOpen() && !state->sha1Valid) {
|
|
|
|
recomputeAndStoreSha1(state, state->file);
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
2017-11-08 03:07:16 +03:00
|
|
|
state->checkInvariants();
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileInode::fsync(bool datasync) {
|
|
|
|
auto state = state_.wlock();
|
2017-11-21 20:14:31 +03:00
|
|
|
if (!state->isFileOpen()) {
|
2017-07-27 09:39:02 +03:00
|
|
|
// If we don't have an overlay file then we have nothing to sync.
|
|
|
|
return;
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
auto res =
|
|
|
|
#ifndef __APPLE__
|
|
|
|
datasync ? ::fdatasync(state->file.fd()) :
|
|
|
|
#endif
|
|
|
|
::fsync(state->file.fd());
|
|
|
|
checkUnixError(res);
|
|
|
|
|
|
|
|
// let's take this opportunity to update the sha1 attribute.
|
2017-11-20 23:03:28 +03:00
|
|
|
// TODO: A program that issues a series of write() and fsync() syscalls (for
|
|
|
|
// example, when logging to a file), would exhibit quadratic behavior here.
|
|
|
|
// This should either not recompute SHA-1 here or instead remember if the
|
|
|
|
// prior SHA-1 was actually used.
|
2017-07-27 09:39:02 +03:00
|
|
|
if (!state->sha1Valid) {
|
2017-11-21 20:14:31 +03:00
|
|
|
recomputeAndStoreSha1(state, state->file);
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 03:46:03 +03:00
|
|
|
folly::Future<std::string> FileInode::readAll() {
|
|
|
|
return ensureDataLoaded().then([self = inodePtrFromThis()] {
|
|
|
|
// We need to take the wlock instead of the rlock because the lseek() call
|
|
|
|
// modifies the file offset of the file descriptor.
|
|
|
|
auto state = self->state_.wlock();
|
|
|
|
std::string result;
|
2017-11-20 23:03:28 +03:00
|
|
|
switch (state->tag) {
|
|
|
|
case State::MATERIALIZED_IN_OVERLAY: {
|
2017-11-21 20:14:31 +03:00
|
|
|
auto file = self->getFile(*state);
|
|
|
|
auto rc = lseek(file.fd(), Overlay::kHeaderLength, SEEK_SET);
|
2017-11-20 23:03:28 +03:00
|
|
|
folly::checkUnixError(rc, "unable to seek in materialized FileInode");
|
2017-11-21 20:14:31 +03:00
|
|
|
folly::readFile(file.fd(), result);
|
2017-11-20 23:03:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case State::BLOB_LOADED: {
|
|
|
|
const auto& contentsBuf = state->blob->getContents();
|
|
|
|
folly::io::Cursor cursor(&contentsBuf);
|
|
|
|
result = cursor.readFixedString(contentsBuf.computeChainDataLength());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
EDEN_BUG()
|
|
|
|
<< "neither materialized nor loaded after ensureDataLoaded()";
|
2017-11-04 03:46:03 +03:00
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-04 03:46:03 +03:00
|
|
|
// We want to update atime after the read operation.
|
2017-12-05 20:55:31 +03:00
|
|
|
state->timeStamps.atime = self->getNow();
|
2017-11-04 03:46:03 +03:00
|
|
|
return result;
|
|
|
|
});
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fusell::BufVec FileInode::read(size_t size, off_t off) {
|
2018-01-04 04:18:28 +03:00
|
|
|
// It's potentially possible here to optimize a fast path here only requiring
|
|
|
|
// a read lock. However, since a write lock is required to update atime and
|
|
|
|
// cache the file handle in the case of a materialized file, do the simple
|
|
|
|
// thing and just acquire a write lock.
|
|
|
|
|
|
|
|
auto state = state_.wlock();
|
2018-01-04 04:18:30 +03:00
|
|
|
state->checkInvariants();
|
2018-01-04 04:18:28 +03:00
|
|
|
SCOPE_SUCCESS {
|
|
|
|
state->timeStamps.atime = getNow();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (state->tag == State::MATERIALIZED_IN_OVERLAY) {
|
|
|
|
auto file = getFile(*state);
|
|
|
|
auto buf = folly::IOBuf::createCombined(size);
|
|
|
|
auto res = ::pread(
|
|
|
|
file.fd(), buf->writableBuffer(), size, off + Overlay::kHeaderLength);
|
|
|
|
|
|
|
|
checkUnixError(res);
|
|
|
|
buf->append(res);
|
|
|
|
return fusell::BufVec{std::move(buf)};
|
|
|
|
} else {
|
2018-01-04 04:18:30 +03:00
|
|
|
// read() is either called by the FileHandle or FileInode. They must
|
|
|
|
// guarantee openCount > 0.
|
|
|
|
CHECK(state->blob);
|
2018-01-04 04:18:28 +03:00
|
|
|
auto buf = state->blob->getContents();
|
|
|
|
folly::io::Cursor cursor(&buf);
|
|
|
|
|
|
|
|
if (!cursor.canAdvance(off)) {
|
|
|
|
// Seek beyond EOF. Return an empty result.
|
|
|
|
return fusell::BufVec{folly::IOBuf::wrapBuffer("", 0)};
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.skip(off);
|
|
|
|
|
|
|
|
std::unique_ptr<folly::IOBuf> result;
|
|
|
|
cursor.cloneAtMost(result, size);
|
|
|
|
|
|
|
|
return fusell::BufVec{std::move(result)};
|
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 22:08:56 +03:00
|
|
|
folly::Future<size_t> FileInode::write(fusell::BufVec&& buf, off_t off) {
|
2017-07-27 09:39:02 +03:00
|
|
|
auto state = state_.wlock();
|
2017-08-15 09:07:53 +03:00
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
if (State::MATERIALIZED_IN_OVERLAY != state->tag) {
|
2017-12-05 22:08:56 +03:00
|
|
|
// Not open for write, so wait until it is.
|
|
|
|
return materializeForWrite().then(
|
|
|
|
[self = inodePtrFromThis(), buf = buf.copyData(), off]() mutable {
|
|
|
|
return self->write(StringPiece{buf}, off);
|
|
|
|
});
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
2017-12-05 22:08:56 +03:00
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
auto file = getFile(*state);
|
2017-07-27 09:39:02 +03:00
|
|
|
|
|
|
|
state->sha1Valid = false;
|
|
|
|
auto vec = buf.getIov();
|
|
|
|
auto xfer = ::pwritev(
|
2017-11-21 20:14:31 +03:00
|
|
|
file.fd(), vec.data(), vec.size(), off + Overlay::kHeaderLength);
|
2017-07-27 09:39:02 +03:00
|
|
|
checkUnixError(xfer);
|
2017-08-15 09:07:53 +03:00
|
|
|
|
|
|
|
// Update mtime and ctime on write systemcall.
|
2018-01-24 01:48:59 +03:00
|
|
|
const auto now = getNow();
|
2017-12-05 20:55:31 +03:00
|
|
|
state->timeStamps.mtime = now;
|
|
|
|
state->timeStamps.ctime = now;
|
2017-08-15 09:07:53 +03:00
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
return xfer;
|
|
|
|
}
|
|
|
|
|
2017-12-05 22:08:56 +03:00
|
|
|
folly::Future<size_t> FileInode::write(folly::StringPiece data, off_t off) {
|
2017-07-27 09:39:02 +03:00
|
|
|
auto state = state_.wlock();
|
2017-08-15 09:07:53 +03:00
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
if (State::MATERIALIZED_IN_OVERLAY != state->tag) {
|
2017-12-05 22:08:56 +03:00
|
|
|
// Not open for write, so wait until it is.
|
|
|
|
return materializeForWrite().then(
|
|
|
|
[self = inodePtrFromThis(), data = data.str(), off]() mutable {
|
|
|
|
return self->write(StringPiece{data}, off);
|
|
|
|
});
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
2017-11-21 20:14:31 +03:00
|
|
|
auto file = getFile(*state);
|
2017-07-27 09:39:02 +03:00
|
|
|
|
|
|
|
state->sha1Valid = false;
|
|
|
|
auto xfer = ::pwrite(
|
2017-11-21 20:14:31 +03:00
|
|
|
file.fd(), data.data(), data.size(), off + Overlay::kHeaderLength);
|
2017-07-27 09:39:02 +03:00
|
|
|
checkUnixError(xfer);
|
2017-08-15 09:07:53 +03:00
|
|
|
|
|
|
|
// Update mtime and ctime on write systemcall.
|
2018-01-24 01:48:59 +03:00
|
|
|
const auto now = getNow();
|
2017-12-05 20:55:31 +03:00
|
|
|
state->timeStamps.mtime = now;
|
|
|
|
state->timeStamps.ctime = now;
|
2017-08-15 09:07:53 +03:00
|
|
|
|
2017-07-27 09:39:02 +03:00
|
|
|
return xfer;
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:23:26 +03:00
|
|
|
// Waits until inode is either in 'loaded' or 'materialized' state.
|
2018-01-04 04:18:26 +03:00
|
|
|
Future<FileInode::FileHandlePtr> FileInode::ensureDataLoaded() {
|
2018-01-04 04:18:32 +03:00
|
|
|
folly::Optional<Future<FileHandlePtr>> resultFuture;
|
2017-11-11 00:23:26 +03:00
|
|
|
auto blobFuture = Future<std::shared_ptr<const Blob>>::makeEmpty();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-11 00:23:26 +03:00
|
|
|
{
|
|
|
|
// Scope the lock so that we can't deadlock on the completion of
|
|
|
|
// the blobFuture below.
|
|
|
|
auto state = state_.wlock();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
state->checkInvariants();
|
2017-11-11 00:23:26 +03:00
|
|
|
SCOPE_SUCCESS {
|
|
|
|
state->checkInvariants();
|
|
|
|
};
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-20 23:03:28 +03:00
|
|
|
switch (state->tag) {
|
|
|
|
case State::BLOB_LOADING:
|
|
|
|
// If we're already loading, latch on to the in-progress load
|
|
|
|
return state->blobLoadingPromise->getFuture();
|
|
|
|
|
|
|
|
case State::BLOB_LOADED:
|
|
|
|
case State::MATERIALIZED_IN_OVERLAY:
|
|
|
|
// Nothing to do if loaded or materialized.
|
2018-01-04 04:18:26 +03:00
|
|
|
return makeFuture(std::make_shared<FileHandle>(
|
|
|
|
inodePtrFromThis(), [&state] { fileHandleDidOpen(*state); }));
|
2017-11-20 23:03:28 +03:00
|
|
|
|
|
|
|
case State::NOT_LOADED:
|
|
|
|
// Start the blob load first in case this throws an exception.
|
|
|
|
// Ideally the state transition is no-except in tandem with the
|
|
|
|
// Future's .then call.
|
|
|
|
blobFuture = getObjectStore()->getBlob(state->hash.value());
|
|
|
|
|
|
|
|
// We need to load the blob data. Arrange to do so in a way that
|
|
|
|
// multiple callers can wait for.
|
2018-01-04 04:18:26 +03:00
|
|
|
folly::SharedPromise<FileHandlePtr> promise;
|
2017-11-20 23:03:28 +03:00
|
|
|
// The resultFuture will complete after we have loaded the blob
|
|
|
|
// and updated state_.
|
|
|
|
resultFuture = promise.getFuture();
|
|
|
|
|
|
|
|
// Everything from here through blobFuture.then should be noexcept.
|
|
|
|
state->blobLoadingPromise.emplace(std::move(promise));
|
|
|
|
state->tag = State::BLOB_LOADING;
|
|
|
|
break;
|
2017-11-11 00:23:26 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 03:07:16 +03:00
|
|
|
|
2018-01-04 04:18:32 +03:00
|
|
|
// Execution only gets here in the NOT_LOADED case, in which case resultFuture
|
|
|
|
// is initialized.
|
|
|
|
CHECK(resultFuture);
|
|
|
|
|
2017-11-11 00:23:26 +03:00
|
|
|
auto self = inodePtrFromThis(); // separate line for formatting
|
2017-12-22 23:27:48 +03:00
|
|
|
blobFuture
|
|
|
|
.then([self](folly::Try<std::shared_ptr<const Blob>> tryBlob) {
|
|
|
|
auto state = self->state_.wlock();
|
2017-11-11 00:23:26 +03:00
|
|
|
state->checkInvariants();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-12-22 23:27:48 +03:00
|
|
|
switch (state->tag) {
|
|
|
|
// Since the load doesn't hold the state lock for its duration,
|
|
|
|
// sanity check that the inode is still in loading state.
|
|
|
|
//
|
|
|
|
// Note that FileInode can transition from loading to materialized
|
|
|
|
// with a concurrent materializeForWrite(O_TRUNC), in which case the
|
|
|
|
// state would have transitioned to 'materialized' before this
|
|
|
|
// callback runs.
|
2018-01-04 04:18:32 +03:00
|
|
|
case State::BLOB_LOADING: {
|
|
|
|
auto promise = std::move(*state->blobLoadingPromise);
|
2017-12-22 23:27:48 +03:00
|
|
|
state->blobLoadingPromise.clear();
|
|
|
|
|
|
|
|
if (tryBlob.hasValue()) {
|
|
|
|
// Transition to 'loaded' state.
|
|
|
|
state->blob = std::move(tryBlob.value());
|
|
|
|
state->tag = State::BLOB_LOADED;
|
|
|
|
state->checkInvariants();
|
2018-01-04 04:18:26 +03:00
|
|
|
// The FileHandle must be allocated while the lock is held so the
|
|
|
|
// blob field is set and the openCount incremented atomically, so
|
|
|
|
// that no other thread can cause the blob to get unset before
|
|
|
|
// openCount is incremented.
|
|
|
|
auto result = std::make_shared<FileHandle>(
|
|
|
|
self, [&state] { fileHandleDidOpen(*state); });
|
2017-12-22 23:27:48 +03:00
|
|
|
// Call the Future's subscribers while the state_ lock is not
|
|
|
|
// held. Even if the FileInode has transitioned to a materialized
|
|
|
|
// state, any pending loads must be unblocked.
|
|
|
|
state.unlock();
|
2018-01-04 04:18:26 +03:00
|
|
|
promise.setValue(std::move(result));
|
2017-12-22 23:27:48 +03:00
|
|
|
} else {
|
|
|
|
state->tag = State::NOT_LOADED;
|
|
|
|
state->checkInvariants();
|
|
|
|
// Call the Future's subscribers while the state_ lock is not
|
|
|
|
// held. Even if the FileInode has transitioned to a materialized
|
|
|
|
// state, any pending loads must be unblocked.
|
|
|
|
state.unlock();
|
|
|
|
promise.setException(tryBlob.exception());
|
|
|
|
}
|
|
|
|
break;
|
2018-01-04 04:18:32 +03:00
|
|
|
}
|
2017-12-22 23:27:48 +03:00
|
|
|
|
2018-01-04 04:18:32 +03:00
|
|
|
case State::MATERIALIZED_IN_OVERLAY:
|
2017-12-22 23:27:48 +03:00
|
|
|
// The load raced with a materializeForWrite(O_TRUNC). Nothing left
|
|
|
|
// to do here: ensureDataLoaded() guarantees `blob` or `file` is
|
|
|
|
// defined after its completion, and the
|
|
|
|
// materializeForWrite(O_TRUNC) fulfilled the promise.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
EDEN_BUG()
|
|
|
|
<< "Inode left in unexpected state after getBlob() completed";
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.onError([](const std::exception&) {
|
|
|
|
// We get here if EDEN_BUG() didn't terminate the process, or if we
|
2018-01-04 04:18:32 +03:00
|
|
|
// threw in the preceding block. Both are bad because we won't
|
2017-12-22 23:27:48 +03:00
|
|
|
// automatically propagate the exception to resultFuture and we
|
|
|
|
// can't trust the state of anything if we get here.
|
|
|
|
// Rather than leaving something hanging, we suicide.
|
|
|
|
// We could probably do a bit better with the error handling here :-/
|
|
|
|
XLOG(FATAL)
|
|
|
|
<< "Failed to propagate failure in getBlob(), no choice but to die";
|
|
|
|
});
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2018-01-04 04:18:32 +03:00
|
|
|
return std::move(*resultFuture);
|
2017-11-11 00:23:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
folly::IOBuf createOverlayHeaderFromTimestamps(
|
2018-02-01 23:21:03 +03:00
|
|
|
const InodeTimestamps& timestamps) {
|
2017-11-11 00:23:26 +03:00
|
|
|
return Overlay::createHeader(
|
2018-02-08 02:01:01 +03:00
|
|
|
Overlay::kHeaderIdentifierFile, Overlay::kHeaderVersion, timestamps);
|
2017-11-11 00:23:26 +03:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-12-05 02:07:44 +03:00
|
|
|
Future<Unit> FileInode::materializeForWrite() {
|
2017-11-11 00:23:26 +03:00
|
|
|
// Not O_TRUNC, so ensure we have a blob (or are already materialized).
|
|
|
|
return ensureDataLoaded().then([self = inodePtrFromThis()]() {
|
2017-12-05 20:52:56 +03:00
|
|
|
// Notifying the parent of materialization must happen outside of the lock.
|
|
|
|
{
|
|
|
|
auto state = self->state_.wlock();
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-11-11 00:23:26 +03:00
|
|
|
state->checkInvariants();
|
2017-12-05 20:52:56 +03:00
|
|
|
SCOPE_SUCCESS {
|
|
|
|
state->checkInvariants();
|
|
|
|
};
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
if (state->tag == State::MATERIALIZED_IN_OVERLAY) {
|
|
|
|
// This conditional will be hit if materializeForWrite is called, issues
|
|
|
|
// a load, and then materializeAndTruncate is called before
|
|
|
|
// ensureDataLoaded() completes. The prior O_TRUNC would have completed
|
|
|
|
// synchronously and switched the inode into the 'materialized' state,
|
|
|
|
// in which case there is nothing left to do here.
|
|
|
|
return;
|
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// Add header to the overlay File.
|
|
|
|
auto header = createOverlayHeaderFromTimestamps(state->timeStamps);
|
|
|
|
auto iov = header.getIov();
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
auto filePath = self->getLocalPath();
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// state->blob is guaranteed non-null because:
|
|
|
|
// If state->file was set, we would have early exited above.
|
|
|
|
// If not O_TRUNC, then we called ensureDataLoaded().
|
|
|
|
CHECK_NOTNULL(state->blob.get());
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// Write the blob contents out to the overlay
|
|
|
|
auto contents = state->blob->getContents().getIov();
|
|
|
|
iov.insert(iov.end(), contents.begin(), contents.end());
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
folly::writeFileAtomic(
|
|
|
|
filePath.stringPiece(), iov.data(), iov.size(), 0600);
|
|
|
|
InodeTimestamps timeStamps;
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
auto file = Overlay::openFile(
|
|
|
|
filePath.stringPiece(), Overlay::kHeaderIdentifierFile, timeStamps);
|
|
|
|
state->sha1Valid = false;
|
2017-11-10 20:05:43 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// If we have a SHA-1 from the metadata, apply it to the new file. This
|
|
|
|
// saves us from recomputing it again in the case that something opens the
|
|
|
|
// file read/write and closes it without changing it.
|
|
|
|
auto metadata =
|
|
|
|
self->getObjectStore()->getBlobMetadata(state->hash.value());
|
|
|
|
if (metadata.isReady()) {
|
|
|
|
self->storeSha1(state, file, metadata.value().sha1);
|
|
|
|
} else {
|
|
|
|
// Leave the SHA-1 attribute dirty - it is not very likely that a file
|
|
|
|
// will be opened for writing, closed without changing, and then have
|
|
|
|
// its SHA-1 queried via Thrift or xattr. If so, the SHA-1 will be
|
|
|
|
// recomputed as needed. That said, it's perhaps cheaper to hash now
|
|
|
|
// (SHA-1 is hundreds of MB/s) while the data is accessible in the blob
|
|
|
|
// than to read the file out of the overlay later.
|
|
|
|
}
|
2017-07-27 09:39:02 +03:00
|
|
|
|
2018-01-04 04:18:30 +03:00
|
|
|
// ensureDataLoaded() returns a FileHandle; therefore openCount must be
|
|
|
|
// positive; therefore it's okay to set file.
|
|
|
|
CHECK_GT(state->openCount, 0);
|
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// Update the FileInode to indicate that we are materialized now.
|
|
|
|
state->blob.reset();
|
|
|
|
state->hash = folly::none;
|
2018-01-04 04:18:30 +03:00
|
|
|
state->file = std::move(file);
|
2017-12-05 20:52:56 +03:00
|
|
|
state->tag = State::MATERIALIZED_IN_OVERLAY;
|
2017-12-05 02:07:44 +03:00
|
|
|
}
|
2017-12-05 20:52:56 +03:00
|
|
|
self->materializeInParent();
|
2017-11-11 00:23:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileInode::materializeAndTruncate() {
|
|
|
|
// Set if in 'loading' state. Fulfilled outside of the scopes of any locks.
|
2018-01-04 04:18:32 +03:00
|
|
|
folly::SharedPromise<FileHandlePtr> sharedPromise;
|
2017-11-11 00:23:26 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
// Notifying the parent of materialization must occur outside of the lock.
|
|
|
|
bool didMaterialize = false;
|
|
|
|
|
2018-01-04 04:18:26 +03:00
|
|
|
auto try_ = folly::makeTryWith([&] {
|
2017-11-11 00:23:26 +03:00
|
|
|
auto state = state_.wlock();
|
|
|
|
state->checkInvariants();
|
|
|
|
SCOPE_SUCCESS {
|
|
|
|
state->checkInvariants();
|
|
|
|
};
|
|
|
|
|
2017-11-21 20:14:31 +03:00
|
|
|
folly::File file;
|
|
|
|
if (state->isMaterialized()) { // Materialized already.
|
|
|
|
file = getFile(*state);
|
2017-11-11 00:23:26 +03:00
|
|
|
state->sha1Valid = false;
|
2017-11-21 20:14:31 +03:00
|
|
|
checkUnixError(ftruncate(file.fd(), Overlay::kHeaderLength));
|
2017-11-11 00:23:26 +03:00
|
|
|
// The timestamps in the overlay header will get updated when the inode is
|
|
|
|
// unloaded.
|
|
|
|
} else {
|
|
|
|
// Add header to the overlay File.
|
|
|
|
auto header = createOverlayHeaderFromTimestamps(state->timeStamps);
|
|
|
|
auto iov = header.getIov();
|
|
|
|
|
|
|
|
auto filePath = getLocalPath();
|
|
|
|
|
|
|
|
folly::writeFileAtomic(filePath.stringPiece(), iov.data(), iov.size());
|
|
|
|
// We don't want to set the in-memory timestamps to the timestamps
|
|
|
|
// returned by the below openFile function as we just wrote these
|
|
|
|
// timestamps in to overlay using writeFileAtomic.
|
|
|
|
InodeTimestamps timeStamps;
|
2017-11-21 20:14:31 +03:00
|
|
|
file = Overlay::openFile(
|
2017-11-11 00:23:26 +03:00
|
|
|
filePath.stringPiece(), Overlay::kHeaderIdentifierFile, timeStamps);
|
|
|
|
|
|
|
|
// Everything below here in the scope should be noexcept to ensure that
|
|
|
|
// the state is never partially transitioned.
|
|
|
|
|
|
|
|
// Transition to `loaded`.
|
|
|
|
if (state->blobLoadingPromise) { // Loading.
|
|
|
|
// Move the promise out so it's fulfilled outside of the lock.
|
2018-01-04 04:18:32 +03:00
|
|
|
sharedPromise = std::move(*state->blobLoadingPromise);
|
2017-11-11 00:23:26 +03:00
|
|
|
state->blobLoadingPromise.reset();
|
|
|
|
} else if (state->blob) { // Loaded.
|
|
|
|
state->blob.reset();
|
|
|
|
} else { // Not loaded.
|
|
|
|
}
|
|
|
|
|
|
|
|
state->hash.reset();
|
2017-12-05 02:07:44 +03:00
|
|
|
// If a FileHandle is already open cache the newly-opened file.
|
|
|
|
if (state->openCount) {
|
|
|
|
state->file = std::move(file);
|
|
|
|
}
|
2017-11-11 00:23:26 +03:00
|
|
|
state->sha1Valid = false;
|
2017-11-20 23:03:28 +03:00
|
|
|
state->tag = State::MATERIALIZED_IN_OVERLAY;
|
2017-12-05 20:52:56 +03:00
|
|
|
didMaterialize = true;
|
2017-11-11 00:23:26 +03:00
|
|
|
}
|
2017-11-21 20:14:31 +03:00
|
|
|
storeSha1(state, file, Hash::sha1(ByteRange{}));
|
2018-01-04 04:18:26 +03:00
|
|
|
|
|
|
|
return std::make_shared<FileHandle>(
|
|
|
|
inodePtrFromThis(), [&state] { fileHandleDidOpen(*state); });
|
2017-11-11 00:23:26 +03:00
|
|
|
});
|
2017-11-10 02:51:44 +03:00
|
|
|
|
2017-12-05 20:52:56 +03:00
|
|
|
if (didMaterialize) {
|
|
|
|
materializeInParent();
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:23:26 +03:00
|
|
|
// Fulfill outside of the lock.
|
2018-01-04 04:18:32 +03:00
|
|
|
sharedPromise.setTry(std::move(try_));
|
2017-07-27 09:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStore* FileInode::getObjectStore() const {
|
|
|
|
return getMount()->getObjectStore();
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash FileInode::recomputeAndStoreSha1(
|
2017-11-21 20:14:31 +03:00
|
|
|
const folly::Synchronized<FileInode::State>::LockedPtr& state,
|
2017-12-05 02:07:44 +03:00
|
|
|
const folly::File& file) {
|
2017-07-27 09:39:02 +03:00
|
|
|
uint8_t buf[8192];
|
|
|
|
off_t off = Overlay::kHeaderLength;
|
|
|
|
SHA_CTX ctx;
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Using pread here so that we don't move the file position;
|
|
|
|
// the file descriptor is shared between multiple file handles
|
|
|
|
// and while we serialize the requests to FileData, it seems
|
|
|
|
// like a good property of this function to avoid changing that
|
|
|
|
// state.
|
2017-11-21 20:14:31 +03:00
|
|
|
auto len = folly::preadNoInt(file.fd(), buf, sizeof(buf), off);
|
2017-07-27 09:39:02 +03:00
|
|
|
if (len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len == -1) {
|
|
|
|
folly::throwSystemError();
|
|
|
|
}
|
|
|
|
SHA1_Update(&ctx, buf, len);
|
|
|
|
off += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t digest[SHA_DIGEST_LENGTH];
|
|
|
|
SHA1_Final(digest, &ctx);
|
|
|
|
auto sha1 = Hash(folly::ByteRange(digest, sizeof(digest)));
|
2017-11-21 20:14:31 +03:00
|
|
|
storeSha1(state, file, sha1);
|
2017-07-27 09:39:02 +03:00
|
|
|
return sha1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileInode::storeSha1(
|
|
|
|
const folly::Synchronized<FileInode::State>::LockedPtr& state,
|
2017-11-21 20:14:31 +03:00
|
|
|
const folly::File& file,
|
2017-07-27 09:39:02 +03:00
|
|
|
Hash sha1) {
|
|
|
|
try {
|
2017-11-21 20:14:31 +03:00
|
|
|
fsetxattr(file.fd(), kXattrSha1, sha1.toString());
|
2017-07-27 09:39:02 +03:00
|
|
|
state->sha1Valid = true;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
// If something goes wrong storing the attribute just log a warning
|
|
|
|
// and leave sha1Valid as false. We'll have to recompute the value
|
|
|
|
// next time we need it.
|
|
|
|
XLOG(WARNING) << "error setting SHA1 attribute in the overlay: "
|
|
|
|
<< folly::exceptionStr(ex);
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
2017-07-28 04:12:48 +03:00
|
|
|
|
2017-11-04 03:46:03 +03:00
|
|
|
// Gets the in-memory timestamps of the inode.
|
2018-02-01 23:21:03 +03:00
|
|
|
InodeTimestamps FileInode::getTimestamps() const {
|
|
|
|
return state_.rlock()->timeStamps;
|
2017-07-28 04:12:48 +03:00
|
|
|
}
|
2017-08-05 06:14:18 +03:00
|
|
|
|
2017-09-19 20:59:46 +03:00
|
|
|
folly::Future<folly::Unit> FileInode::prefetch() {
|
|
|
|
// Careful to only hold the lock while fetching a copy of the hash.
|
|
|
|
return folly::via(getMount()->getThreadPool().get()).then([this] {
|
|
|
|
if (auto hash = state_.rlock()->hash) {
|
|
|
|
getObjectStore()->getBlobMetadata(*hash);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-05 06:14:18 +03:00
|
|
|
void FileInode::updateOverlayHeader() const {
|
|
|
|
auto state = state_.wlock();
|
2017-11-21 20:14:31 +03:00
|
|
|
if (state->isMaterialized()) {
|
|
|
|
int fd;
|
|
|
|
folly::File temporaryHandle;
|
|
|
|
if (state->isFileOpen()) {
|
|
|
|
fd = state->file.fd();
|
|
|
|
} else {
|
|
|
|
// We don't have and shouldn't keep a file around, so we return
|
|
|
|
// a temporary file instead.
|
|
|
|
temporaryHandle = folly::File(getLocalPath().c_str(), O_RDWR);
|
|
|
|
fd = temporaryHandle.fd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Overlay::updateTimestampToHeader(fd, state->timeStamps);
|
2017-08-05 06:14:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|