2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2015-10-14 08:14:53 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2015-10-23 14:36:22 +00:00
|
|
|
#pragma once
|
2016-09-13 19:08:22 +00:00
|
|
|
#include <errno.h>
|
2015-10-14 08:14:53 +00:00
|
|
|
#include <unistd.h>
|
2016-04-21 17:37:27 +00:00
|
|
|
#include <atomic>
|
2016-09-13 19:08:22 +00:00
|
|
|
#include <string>
|
2015-10-14 08:14:53 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
|
|
|
|
// For non linux platform, the following macros are used only as place
|
|
|
|
// holder.
|
|
|
|
#if !(defined OS_LINUX) && !(defined CYGWIN)
|
|
|
|
#define POSIX_FADV_NORMAL 0 /* [MC1] no further special treatment */
|
|
|
|
#define POSIX_FADV_RANDOM 1 /* [MC1] expect random page refs */
|
|
|
|
#define POSIX_FADV_SEQUENTIAL 2 /* [MC1] expect sequential page refs */
|
|
|
|
#define POSIX_FADV_WILLNEED 3 /* [MC1] will need these pages */
|
|
|
|
#define POSIX_FADV_DONTNEED 4 /* [MC1] dont need these pages */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
static Status IOError(const std::string& context, int err_number) {
|
2016-09-07 19:37:45 +00:00
|
|
|
return (err_number == ENOSPC) ?
|
|
|
|
Status::NoSpace(context, strerror(err_number)) :
|
|
|
|
Status::IOError(context, strerror(err_number));
|
2015-10-14 08:14:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
class PosixHelper {
|
|
|
|
public:
|
|
|
|
static size_t GetUniqueIdFromFile(int fd, char* id, size_t max_size);
|
|
|
|
};
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
class PosixSequentialFile : public SequentialFile {
|
|
|
|
private:
|
|
|
|
std::string filename_;
|
|
|
|
FILE* file_;
|
|
|
|
int fd_;
|
|
|
|
bool use_os_buffer_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PosixSequentialFile(const std::string& fname, FILE* f,
|
|
|
|
const EnvOptions& options);
|
|
|
|
virtual ~PosixSequentialFile();
|
|
|
|
|
|
|
|
virtual Status Read(size_t n, Slice* result, char* scratch) override;
|
|
|
|
virtual Status Skip(uint64_t n) override;
|
|
|
|
virtual Status InvalidateCache(size_t offset, size_t length) override;
|
|
|
|
};
|
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
class PosixDirectIOSequentialFile : public SequentialFile {
|
|
|
|
public:
|
|
|
|
explicit PosixDirectIOSequentialFile(const std::string& filename, int fd)
|
|
|
|
: filename_(filename), fd_(fd) {}
|
|
|
|
|
|
|
|
virtual ~PosixDirectIOSequentialFile() {}
|
|
|
|
|
|
|
|
Status Read(size_t n, Slice* result, char* scratch) override;
|
|
|
|
Status Skip(uint64_t n) override;
|
|
|
|
Status InvalidateCache(size_t offset, size_t length) override;
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
private:
|
2016-04-21 17:37:27 +00:00
|
|
|
const std::string filename_;
|
|
|
|
int fd_ = -1;
|
|
|
|
std::atomic<size_t> off_{0}; // read offset
|
|
|
|
};
|
|
|
|
|
|
|
|
class PosixRandomAccessFile : public RandomAccessFile {
|
|
|
|
protected:
|
2015-10-14 08:14:53 +00:00
|
|
|
std::string filename_;
|
|
|
|
int fd_;
|
|
|
|
bool use_os_buffer_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PosixRandomAccessFile(const std::string& fname, int fd,
|
|
|
|
const EnvOptions& options);
|
|
|
|
virtual ~PosixRandomAccessFile();
|
|
|
|
|
|
|
|
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override;
|
2016-05-18 23:21:29 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_MACOSX)
|
2015-10-14 08:14:53 +00:00
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
|
|
|
#endif
|
|
|
|
virtual void Hint(AccessPattern pattern) override;
|
|
|
|
virtual Status InvalidateCache(size_t offset, size_t length) override;
|
|
|
|
};
|
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
// Direct IO random access file direct IO implementation
|
|
|
|
class PosixDirectIORandomAccessFile : public PosixRandomAccessFile {
|
|
|
|
public:
|
|
|
|
explicit PosixDirectIORandomAccessFile(const std::string& filename, int fd)
|
|
|
|
: PosixRandomAccessFile(filename, fd, EnvOptions()) {}
|
|
|
|
virtual ~PosixDirectIORandomAccessFile() {}
|
|
|
|
|
|
|
|
Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override;
|
|
|
|
virtual void Hint(AccessPattern pattern) override {}
|
|
|
|
Status InvalidateCache(size_t offset, size_t length) override {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
class PosixWritableFile : public WritableFile {
|
2016-04-21 17:37:27 +00:00
|
|
|
protected:
|
2015-10-14 08:14:53 +00:00
|
|
|
const std::string filename_;
|
|
|
|
int fd_;
|
|
|
|
uint64_t filesize_;
|
2015-10-28 18:55:20 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2015-10-14 08:14:53 +00:00
|
|
|
bool allow_fallocate_;
|
|
|
|
bool fallocate_with_keep_size_;
|
2015-10-28 18:55:20 +00:00
|
|
|
#endif
|
2015-10-14 08:14:53 +00:00
|
|
|
|
|
|
|
public:
|
2016-04-21 17:37:27 +00:00
|
|
|
explicit PosixWritableFile(const std::string& fname, int fd,
|
|
|
|
const EnvOptions& options);
|
|
|
|
virtual ~PosixWritableFile();
|
2015-10-14 08:14:53 +00:00
|
|
|
|
|
|
|
// Means Close() will properly take care of truncate
|
|
|
|
// and it does not need any additional information
|
|
|
|
virtual Status Truncate(uint64_t size) override { return Status::OK(); }
|
|
|
|
virtual Status Close() override;
|
|
|
|
virtual Status Append(const Slice& data) override;
|
|
|
|
virtual Status Flush() override;
|
|
|
|
virtual Status Sync() override;
|
|
|
|
virtual Status Fsync() override;
|
|
|
|
virtual bool IsSyncThreadSafe() const override;
|
|
|
|
virtual uint64_t GetFileSize() override;
|
|
|
|
virtual Status InvalidateCache(size_t offset, size_t length) override;
|
2015-10-28 18:55:20 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2015-11-11 01:03:42 +00:00
|
|
|
virtual Status Allocate(uint64_t offset, uint64_t len) override;
|
|
|
|
virtual Status RangeSync(uint64_t offset, uint64_t nbytes) override;
|
2015-10-14 08:14:53 +00:00
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
2015-10-28 18:55:20 +00:00
|
|
|
#endif
|
2015-10-14 08:14:53 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
class PosixDirectIOWritableFile : public PosixWritableFile {
|
|
|
|
public:
|
|
|
|
explicit PosixDirectIOWritableFile(const std::string& filename, int fd)
|
|
|
|
: PosixWritableFile(filename, fd, EnvOptions()) {}
|
|
|
|
virtual ~PosixDirectIOWritableFile() {}
|
|
|
|
|
|
|
|
bool UseOSBuffer() const override { return false; }
|
|
|
|
size_t GetRequiredBufferAlignment() const override { return 4 * 1024; }
|
|
|
|
Status Append(const Slice& data) override;
|
|
|
|
Status PositionedAppend(const Slice& data, uint64_t offset) override;
|
|
|
|
bool UseDirectIO() const override { return true; }
|
|
|
|
Status InvalidateCache(size_t offset, size_t length) override {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
class PosixMmapReadableFile : public RandomAccessFile {
|
|
|
|
private:
|
|
|
|
int fd_;
|
|
|
|
std::string filename_;
|
|
|
|
void* mmapped_region_;
|
|
|
|
size_t length_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PosixMmapReadableFile(const int fd, const std::string& fname, void* base,
|
|
|
|
size_t length, const EnvOptions& options);
|
|
|
|
virtual ~PosixMmapReadableFile();
|
|
|
|
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override;
|
|
|
|
virtual Status InvalidateCache(size_t offset, size_t length) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PosixMmapFile : public WritableFile {
|
|
|
|
private:
|
|
|
|
std::string filename_;
|
|
|
|
int fd_;
|
|
|
|
size_t page_size_;
|
|
|
|
size_t map_size_; // How much extra memory to map at a time
|
|
|
|
char* base_; // The mapped region
|
|
|
|
char* limit_; // Limit of the mapped region
|
|
|
|
char* dst_; // Where to write next (in range [base_,limit_])
|
|
|
|
char* last_sync_; // Where have we synced up to
|
|
|
|
uint64_t file_offset_; // Offset of base_ in file
|
2015-10-28 18:55:20 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2015-10-14 08:14:53 +00:00
|
|
|
bool allow_fallocate_; // If false, fallocate calls are bypassed
|
|
|
|
bool fallocate_with_keep_size_;
|
2015-10-28 18:55:20 +00:00
|
|
|
#endif
|
2015-10-14 08:14:53 +00:00
|
|
|
|
|
|
|
// Roundup x to a multiple of y
|
|
|
|
static size_t Roundup(size_t x, size_t y) { return ((x + y - 1) / y) * y; }
|
|
|
|
|
|
|
|
size_t TruncateToPageBoundary(size_t s) {
|
|
|
|
s -= (s & (page_size_ - 1));
|
|
|
|
assert((s % page_size_) == 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status MapNewRegion();
|
|
|
|
Status UnmapCurrentRegion();
|
|
|
|
Status Msync();
|
|
|
|
|
|
|
|
public:
|
|
|
|
PosixMmapFile(const std::string& fname, int fd, size_t page_size,
|
|
|
|
const EnvOptions& options);
|
|
|
|
~PosixMmapFile();
|
|
|
|
|
|
|
|
// Means Close() will properly take care of truncate
|
|
|
|
// and it does not need any additional information
|
|
|
|
virtual Status Truncate(uint64_t size) override { return Status::OK(); }
|
|
|
|
virtual Status Close() override;
|
|
|
|
virtual Status Append(const Slice& data) override;
|
|
|
|
virtual Status Flush() override;
|
|
|
|
virtual Status Sync() override;
|
|
|
|
virtual Status Fsync() override;
|
|
|
|
virtual uint64_t GetFileSize() override;
|
|
|
|
virtual Status InvalidateCache(size_t offset, size_t length) override;
|
2015-10-28 18:55:20 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2015-11-11 01:03:42 +00:00
|
|
|
virtual Status Allocate(uint64_t offset, uint64_t len) override;
|
2015-10-28 18:55:20 +00:00
|
|
|
#endif
|
2015-10-14 08:14:53 +00:00
|
|
|
};
|
|
|
|
|
2016-09-13 19:08:22 +00:00
|
|
|
class PosixRandomRWFile : public RandomRWFile {
|
|
|
|
public:
|
|
|
|
explicit PosixRandomRWFile(const std::string& fname, int fd,
|
|
|
|
const EnvOptions& options);
|
|
|
|
virtual ~PosixRandomRWFile();
|
|
|
|
|
|
|
|
virtual Status Write(uint64_t offset, const Slice& data) override;
|
|
|
|
|
|
|
|
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const override;
|
|
|
|
|
|
|
|
virtual Status Flush() override;
|
|
|
|
virtual Status Sync() override;
|
|
|
|
virtual Status Fsync() override;
|
|
|
|
virtual Status Close() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string filename_;
|
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
class PosixDirectory : public Directory {
|
|
|
|
public:
|
|
|
|
explicit PosixDirectory(int fd) : fd_(fd) {}
|
2015-10-27 19:15:55 +00:00
|
|
|
~PosixDirectory();
|
|
|
|
virtual Status Fsync() override;
|
2015-10-14 08:14:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|