#include "net/disk_cache/blockfile/sparse_control.h"
#include <stdint.h>
#include "base/compiler_specific.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "net/base/interval.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/blockfile/backend_impl.h"
#include "net/disk_cache/blockfile/entry_impl.h"
#include "net/disk_cache/blockfile/file.h"
#include "net/disk_cache/net_log_parameters.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"
using base::Time;
namespace {
const int kSparseIndex = 2;
const int kSparseData = 1;
const int kMaxMapSize = 8 * 1024;
const int kMaxEntrySize = 0x100000;
const int64_t kMaxEndOffset = 8ll * kMaxMapSize * kMaxEntrySize;
const int kBlockSize = 1024;
std::string GenerateChildName(const std::string& base_name,
int64_t signature,
int64_t child_id) {
return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(),
signature, child_id);
}
class ChildrenDeleter
: public base::RefCounted<ChildrenDeleter>,
public disk_cache::FileIOCallback {
public:
ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name)
: backend_(backend->GetWeakPtr()), name_(name) {}
ChildrenDeleter(const ChildrenDeleter&) = delete;
ChildrenDeleter& operator=(const ChildrenDeleter&) = delete;
void OnFileIOComplete(int bytes_copied) override;
void Start(base::HeapArray<uint8_t> buffer, int len);
void ReadData(disk_cache::Addr address, int len);
private:
friend class base::RefCounted<ChildrenDeleter>;
~ChildrenDeleter() override = default;
void DeleteChildren();
base::WeakPtr<disk_cache::BackendImpl> backend_;
std::string name_;
disk_cache::Bitmap children_map_;
int64_t signature_ = 0;
base::HeapArray<uint8_t> buffer_;
};
void ChildrenDeleter::OnFileIOComplete(int bytes_copied) {
Start(std::move(buffer_), bytes_copied);
}
void ChildrenDeleter::Start(base::HeapArray<uint8_t> buffer, int len) {
if (len < static_cast<int>(sizeof(disk_cache::SparseData)))
return Release();
disk_cache::SparseData* data =
reinterpret_cast<disk_cache::SparseData*>(buffer.data());
signature_ = data->header.signature;
int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8;
children_map_.Resize(num_bits, false);
base::span<uint8_t> bitmap_bytes =
buffer.subspan(offsetof(disk_cache::SparseData, bitmap));
children_map_.SetMap(disk_cache::ToUint32Span(bitmap_bytes));
buffer = {};
DeleteChildren();
}
void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) {
DCHECK(address.is_block_file());
if (!backend_.get())
return Release();
disk_cache::File* file(backend_->File(address));
if (!file)
return Release();
size_t file_offset = address.start_block() * address.BlockSize() +
disk_cache::kBlockHeaderSize;
buffer_ = base::HeapArray<uint8_t>::Uninit(len);
bool completed;
if (!file->Read(buffer_.as_span(), file_offset, this, &completed)) {
return Release();
}
if (completed)
OnFileIOComplete(len);
}
void ChildrenDeleter::DeleteChildren() {
int child_id = 0;
if (!children_map_.FindNextSetBit(&child_id) || !backend_.get()) {
return Release();
}
std::string child_name = GenerateChildName(name_, signature_, child_id);
backend_->SyncDoomEntry(child_name);
children_map_.Set(child_id, false);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&ChildrenDeleter::DeleteChildren, this));
}
net::NetLogEventType GetSparseEventType(
disk_cache::SparseControl::SparseOperation operation) {
switch (operation) {
case disk_cache::SparseControl::kReadOperation:
return net::NetLogEventType::SPARSE_READ;
case disk_cache::SparseControl::kWriteOperation:
return net::NetLogEventType::SPARSE_WRITE;
case disk_cache::SparseControl::kGetRangeOperation:
return net::NetLogEventType::SPARSE_GET_RANGE;
default:
NOTREACHED();
}
}
void LogChildOperationEnd(const net::NetLogWithSource& net_log,
disk_cache::SparseControl::SparseOperation operation,
int result) {
if (net_log.IsCapturing()) {
net::NetLogEventType event_type;
switch (operation) {
case disk_cache::SparseControl::kReadOperation:
event_type = net::NetLogEventType::SPARSE_READ_CHILD_DATA;
break;
case disk_cache::SparseControl::kWriteOperation:
event_type = net::NetLogEventType::SPARSE_WRITE_CHILD_DATA;
break;
case disk_cache::SparseControl::kGetRangeOperation:
return;
default:
NOTREACHED();
}
net_log.EndEventWithNetErrorCode(event_type, result);
}
}
}
namespace disk_cache {
SparseControl::SparseControl(EntryImpl* entry)
: entry_(entry),
child_map_(ToUint32Span(base::as_writable_byte_span(child_data_.bitmap)),
kNumSparseBits) {
static_assert(sizeof(child_data_.bitmap) == kNumSparseBits / 8);
}
SparseControl::~SparseControl() {
if (child_)
CloseChild();
if (init_)
WriteSparseData();
}
int SparseControl::Init() {
DCHECK(!init_);
if (entry_->GetDataSize(kSparseData))
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
int rv = net::OK;
int data_len = entry_->GetDataSize(kSparseIndex);
if (!data_len) {
rv = CreateSparseEntry();
} else {
rv = OpenSparseEntry(data_len);
}
if (rv == net::OK)
init_ = true;
return rv;
}
bool SparseControl::CouldBeSparse() const {
DCHECK(!init_);
if (entry_->GetDataSize(kSparseData))
return false;
return (entry_->GetDataSize(kSparseIndex) != 0);
}
int SparseControl::StartIO(SparseOperation op,
int64_t offset,
net::IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK(init_);
if (operation_ != kNoOperation)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (offset < 0 || buf_len < 0)
return net::ERR_INVALID_ARGUMENT;
int64_t end_offset = 0;
if (!base::CheckAdd(offset, buf_len).AssignIfValid(&end_offset)) {
if (op == kWriteOperation)
return net::ERR_INVALID_ARGUMENT;
else
end_offset = std::numeric_limits<int64_t>::max();
}
if (offset >= kMaxEndOffset) {
if (op == kGetRangeOperation)
return 0;
else
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
}
if (end_offset > kMaxEndOffset) {
if (op == kWriteOperation)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
else
end_offset = kMaxEndOffset;
}
DCHECK_GE(end_offset, offset);
buf_len = end_offset - offset;
DCHECK(!user_buf_.get());
DCHECK(user_callback_.is_null());
if (!buf && (op == kReadOperation || op == kWriteOperation))
return 0;
operation_ = op;
offset_ = offset;
user_buf_ = buf ? base::MakeRefCounted<net::DrainableIOBuffer>(buf, buf_len)
: nullptr;
buf_len_ = buf_len;
user_callback_ = std::move(callback);
result_ = 0;
pending_ = false;
finished_ = false;
abort_ = false;
if (entry_->net_log().IsCapturing()) {
NetLogSparseOperation(entry_->net_log(), GetSparseEventType(operation_),
net::NetLogEventPhase::BEGIN, offset_, buf_len_);
}
DoChildrenIO();
if (!pending_) {
operation_ = kNoOperation;
user_buf_ = nullptr;
user_callback_.Reset();
return result_;
}
return net::ERR_IO_PENDING;
}
RangeResult SparseControl::GetAvailableRange(int64_t offset, int len) {
DCHECK(init_);
if (operation_ != kNoOperation)
return RangeResult(net::ERR_CACHE_OPERATION_NOT_SUPPORTED);
range_found_ = false;
int result = StartIO(kGetRangeOperation, offset, nullptr, len,
CompletionOnceCallback());
if (range_found_)
return RangeResult(offset_, result);
if (result < 0)
return RangeResult(static_cast<net::Error>(result));
return RangeResult(offset, 0);
}
void SparseControl::CancelIO() {
if (operation_ == kNoOperation)
return;
abort_ = true;
}
int SparseControl::ReadyToUse(CompletionOnceCallback callback) {
if (!abort_)
return net::OK;
entry_->AddRef();
abort_callbacks_.push_back(std::move(callback));
return net::ERR_IO_PENDING;
}
void SparseControl::DeleteChildren(EntryImpl* entry) {
DCHECK(entry->GetEntryFlags() & PARENT_ENTRY);
int data_len = entry->GetDataSize(kSparseIndex);
if (data_len < static_cast<int>(sizeof(SparseData)) ||
entry->GetDataSize(kSparseData))
return;
int map_len = data_len - sizeof(SparseHeader);
if (map_len > kMaxMapSize || map_len % 4)
return;
base::HeapArray<uint8_t> buffer;
Addr address;
entry->GetData(kSparseIndex, &buffer, &address);
if (buffer.empty() && !address.is_initialized()) {
return;
}
entry->net_log().AddEvent(net::NetLogEventType::SPARSE_DELETE_CHILDREN);
DCHECK(entry->backend_.get());
ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(),
entry->GetKey());
deleter->AddRef();
if (!buffer.empty()) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&ChildrenDeleter::Start, deleter,
std::move(buffer), data_len));
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&ChildrenDeleter::ReadData, deleter, address, data_len));
}
}
int SparseControl::CreateSparseEntry() {
if (CHILD_ENTRY & entry_->GetEntryFlags())
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
sparse_header_ = SparseHeader();
sparse_header_.signature = Time::Now().ToInternalValue();
sparse_header_.magic = kIndexMagic;
sparse_header_.parent_key_len = entry_->GetKey().size();
children_map_.Resize(kNumSparseBits, true);
scoped_refptr<net::IOBuffer> buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(base::span_from_ref(sparse_header_)));
int rv = entry_->WriteData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_),
CompletionOnceCallback(), false);
if (rv != sizeof(sparse_header_)) {
DLOG(ERROR) << "Unable to save sparse_header_";
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
}
entry_->SetEntryFlags(PARENT_ENTRY);
return net::OK;
}
int SparseControl::OpenSparseEntry(int data_len) {
if (data_len < static_cast<int>(sizeof(SparseData)))
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (entry_->GetDataSize(kSparseData))
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (!(PARENT_ENTRY & entry_->GetEntryFlags()))
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
int map_len = data_len - sizeof(sparse_header_);
if (map_len > kMaxMapSize || map_len % 4)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
scoped_refptr<net::IOBuffer> buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(base::span_from_ref(sparse_header_)));
int rv = entry_->ReadData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_),
CompletionOnceCallback());
if (rv != static_cast<int>(sizeof(sparse_header_)))
return net::ERR_CACHE_READ_FAILURE;
if (sparse_header_.magic != kIndexMagic ||
sparse_header_.parent_key_len !=
static_cast<int>(entry_->GetKey().size()))
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
buf = base::MakeRefCounted<net::IOBufferWithSize>(map_len);
rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(),
map_len, CompletionOnceCallback());
if (rv != map_len)
return net::ERR_CACHE_READ_FAILURE;
children_map_.Resize(map_len * 8, false);
children_map_.SetMap(ToUint32Span(buf->span()));
return net::OK;
}
bool SparseControl::OpenChild() {
DCHECK_GE(result_, 0);
std::string key = GenerateChildKey();
if (child_) {
if (key == child_->GetKey())
return true;
CloseChild();
}
if (!ChildPresent())
return ContinueWithoutChild(key);
if (!entry_->backend_.get())
return false;
child_ = entry_->backend_->OpenEntryImpl(key);
if (!child_)
return ContinueWithoutChild(key);
if (!(CHILD_ENTRY & child_->GetEntryFlags()) ||
child_->GetDataSize(kSparseIndex) < static_cast<int>(sizeof(child_data_)))
return KillChildAndContinue(key, false);
auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(base::span_from_ref(child_data_)));
int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
CompletionOnceCallback());
if (rv != sizeof(child_data_))
return KillChildAndContinue(key, true);
if (child_data_.header.signature != sparse_header_.signature ||
child_data_.header.magic != kIndexMagic)
return KillChildAndContinue(key, false);
if (child_data_.header.last_block_len < 0 ||
child_data_.header.last_block_len >= kBlockSize) {
child_data_.header.last_block_len = 0;
child_data_.header.last_block = -1;
}
return true;
}
void SparseControl::CloseChild() {
auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(base::span_from_ref(child_data_)));
int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
CompletionOnceCallback(), false);
if (rv != sizeof(child_data_))
DLOG(ERROR) << "Failed to save child data";
child_ = nullptr;
}
std::string SparseControl::GenerateChildKey() {
return GenerateChildName(entry_->GetKey(), sparse_header_.signature,
offset_ >> 20);
}
bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) {
SetChildBit(false);
child_->DoomImpl();
child_ = nullptr;
if (fatal) {
result_ = net::ERR_CACHE_READ_FAILURE;
return false;
}
return ContinueWithoutChild(key);
}
bool SparseControl::ContinueWithoutChild(const std::string& key) {
if (kReadOperation == operation_)
return false;
if (kGetRangeOperation == operation_)
return true;
if (!entry_->backend_.get())
return false;
child_ = entry_->backend_->CreateEntryImpl(key);
if (!child_) {
child_ = nullptr;
result_ = net::ERR_CACHE_READ_FAILURE;
return false;
}
InitChildData();
return true;
}
bool SparseControl::ChildPresent() {
int child_bit = static_cast<int>(offset_ >> 20);
if (children_map_.Size() <= child_bit)
return false;
return children_map_.Get(child_bit);
}
void SparseControl::SetChildBit(bool value) {
int child_bit = static_cast<int>(offset_ >> 20);
if (children_map_.Size() <= child_bit)
children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true);
children_map_.Set(child_bit, value);
}
void SparseControl::WriteSparseData() {
auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(children_map_.GetSpan()));
int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(),
buf->size(), CompletionOnceCallback(), false);
if (rv != buf->size()) {
DLOG(ERROR) << "Unable to save sparse map";
}
}
bool SparseControl::VerifyRange() {
DCHECK_GE(result_, 0);
child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1);
child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_);
if (operation_ != kReadOperation)
return true;
int last_bit = (child_offset_ + child_len_ + 1023) >> 10;
int start = child_offset_ >> 10;
if (child_map_.FindNextBit(&start, last_bit, false)) {
DCHECK_GE(child_data_.header.last_block_len, 0);
DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
int partial_block_len = PartialBlockLength(start);
if (start == child_offset_ >> 10) {
if (partial_block_len <= (child_offset_ & (kBlockSize - 1)))
return false;
}
child_len_ = (start << 10) - child_offset_;
if (partial_block_len) {
child_len_ = std::min(child_len_ + partial_block_len, buf_len_);
}
buf_len_ = child_len_;
}
return true;
}
void SparseControl::UpdateRange(int result) {
if (result <= 0 || operation_ != kWriteOperation)
return;
DCHECK_GE(child_data_.header.last_block_len, 0);
DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
int first_bit = child_offset_ >> 10;
int block_offset = child_offset_ & (kBlockSize - 1);
if (block_offset && (child_data_.header.last_block != first_bit ||
child_data_.header.last_block_len < block_offset)) {
first_bit++;
}
int last_bit = (child_offset_ + result) >> 10;
block_offset = (child_offset_ + result) & (kBlockSize - 1);
if (first_bit > last_bit)
return;
if (block_offset && !child_map_.Get(last_bit)) {
child_data_.header.last_block = last_bit;
child_data_.header.last_block_len = block_offset;
} else {
child_data_.header.last_block = -1;
}
child_map_.SetRange(first_bit, last_bit, true);
}
int SparseControl::PartialBlockLength(int block_index) const {
if (block_index == child_data_.header.last_block)
return child_data_.header.last_block_len;
return 0;
}
void SparseControl::InitChildData() {
child_->SetEntryFlags(CHILD_ENTRY);
child_data_ = SparseData();
child_data_.header = sparse_header_;
auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
base::as_chars(base::span_from_ref(child_data_)));
int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
CompletionOnceCallback(), false);
if (rv != sizeof(child_data_))
DLOG(ERROR) << "Failed to save child data";
SetChildBit(true);
}
void SparseControl::DoChildrenIO() {
while (DoChildIO()) continue;
if (kGetRangeOperation == operation_ && entry_->net_log().IsCapturing()) {
entry_->net_log().EndEvent(net::NetLogEventType::SPARSE_GET_RANGE, [&] {
return CreateNetLogGetAvailableRangeResultParams(
RangeResult(offset_, result_));
});
}
if (finished_) {
if (kGetRangeOperation != operation_ && entry_->net_log().IsCapturing()) {
entry_->net_log().EndEvent(GetSparseEventType(operation_));
}
if (pending_)
DoUserCallback();
}
}
bool SparseControl::DoChildIO() {
finished_ = true;
if (!buf_len_ || result_ < 0)
return false;
if (!OpenChild())
return false;
if (!VerifyRange())
return false;
finished_ = false;
CompletionOnceCallback callback;
if (!user_callback_.is_null()) {
callback = base::BindOnce(&SparseControl::OnChildIOCompleted,
base::Unretained(this));
}
int rv = 0;
switch (operation_) {
case kReadOperation:
if (entry_->net_log().IsCapturing()) {
NetLogSparseReadWrite(entry_->net_log(),
net::NetLogEventType::SPARSE_READ_CHILD_DATA,
net::NetLogEventPhase::BEGIN,
child_->net_log().source(), child_len_);
}
rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(),
child_len_, std::move(callback));
break;
case kWriteOperation:
if (entry_->net_log().IsCapturing()) {
NetLogSparseReadWrite(entry_->net_log(),
net::NetLogEventType::SPARSE_WRITE_CHILD_DATA,
net::NetLogEventPhase::BEGIN,
child_->net_log().source(), child_len_);
}
rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(),
child_len_, std::move(callback), false);
break;
case kGetRangeOperation:
rv = DoGetAvailableRange();
break;
default:
NOTREACHED();
}
if (rv == net::ERR_IO_PENDING) {
if (!pending_) {
pending_ = true;
entry_->AddRef();
}
return false;
}
if (!rv)
return false;
DoChildIOCompleted(rv);
return true;
}
int SparseControl::DoGetAvailableRange() {
if (!child_)
return child_len_;
net::Interval<int> to_find(child_offset_, child_offset_ + child_len_);
int first_bit = child_offset_ >> 10;
int last_bit = (child_offset_ + child_len_ + kBlockSize - 1) >> 10;
int found = first_bit;
int bits_found = child_map_.FindBits(&found, last_bit, true);
net::Interval<int> bitmap_range(found * kBlockSize,
found * kBlockSize + bits_found * kBlockSize);
net::Interval<int> last_write_range;
if (child_data_.header.last_block >= 0) {
last_write_range =
net::Interval<int>(child_data_.header.last_block * kBlockSize,
child_data_.header.last_block * kBlockSize +
child_data_.header.last_block_len);
}
if (!last_write_range.Empty() && !bitmap_range.Empty() &&
bitmap_range.max() == last_write_range.min()) {
bitmap_range.SetMax(last_write_range.max());
last_write_range.Clear();
}
bitmap_range.IntersectWith(to_find);
last_write_range.IntersectWith(to_find);
net::Interval<int> result_range = bitmap_range;
if (bitmap_range.Empty() || (!last_write_range.Empty() &&
last_write_range.min() < bitmap_range.min()))
result_range = last_write_range;
if (result_range.Empty()) {
return child_len_;
}
range_found_ = true;
offset_ += result_range.min() - child_offset_;
result_ = result_range.max() - result_range.min();
return 0;
}
void SparseControl::DoChildIOCompleted(int result) {
LogChildOperationEnd(entry_->net_log(), operation_, result);
if (result < 0) {
result_ = result;
return;
}
UpdateRange(result);
result_ += result;
offset_ += result;
buf_len_ -= result;
if (buf_len_ && user_buf_.get())
user_buf_->DidConsume(result);
}
void SparseControl::OnChildIOCompleted(int result) {
DCHECK_NE(net::ERR_IO_PENDING, result);
DoChildIOCompleted(result);
if (abort_) {
abort_ = false;
if (entry_->net_log().IsCapturing()) {
entry_->net_log().AddEvent(net::NetLogEventType::CANCELLED);
entry_->net_log().EndEvent(GetSparseEventType(operation_));
}
bool has_abort_callbacks = !abort_callbacks_.empty();
DoUserCallback();
if (has_abort_callbacks)
DoAbortCallbacks();
return;
}
DoChildrenIO();
}
void SparseControl::DoUserCallback() {
DCHECK(!user_callback_.is_null());
CompletionOnceCallback cb = std::move(user_callback_);
user_buf_ = nullptr;
pending_ = false;
operation_ = kNoOperation;
int rv = result_;
entry_->Release();
std::move(cb).Run(rv);
}
void SparseControl::DoAbortCallbacks() {
std::vector<CompletionOnceCallback> abort_callbacks;
abort_callbacks.swap(abort_callbacks_);
for (CompletionOnceCallback& callback : abort_callbacks) {
entry_->Release();
std::move(callback).Run(net::OK);
}
}
}