1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/blockfile/sparse_control.h"
6
7 #include <stdint.h>
8
9 #include "base/format_macros.h"
10 #include "base/functional/bind.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/numerics/checked_math.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/task/single_thread_task_runner.h"
17 #include "base/time/time.h"
18 #include "net/base/interval.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "net/disk_cache/blockfile/backend_impl.h"
22 #include "net/disk_cache/blockfile/entry_impl.h"
23 #include "net/disk_cache/blockfile/file.h"
24 #include "net/disk_cache/net_log_parameters.h"
25 #include "net/log/net_log.h"
26 #include "net/log/net_log_event_type.h"
27 #include "net/log/net_log_with_source.h"
28
29 using base::Time;
30
31 namespace {
32
33 // Stream of the sparse data index.
34 const int kSparseIndex = 2;
35
36 // Stream of the sparse data.
37 const int kSparseData = 1;
38
39 // We can have up to 64k children.
40 const int kMaxMapSize = 8 * 1024;
41
42 // The maximum number of bytes that a child can store.
43 const int kMaxEntrySize = 0x100000;
44
45 // How much we can address. 8 KiB bitmap (kMaxMapSize above) gives us offsets
46 // up to 64 GiB.
47 const int64_t kMaxEndOffset = 8ll * kMaxMapSize * kMaxEntrySize;
48
49 // The size of each data block (tracked by the child allocation bitmap).
50 const int kBlockSize = 1024;
51
52 // Returns the name of a child entry given the base_name and signature of the
53 // parent and the child_id.
54 // If the entry is called entry_name, child entries will be named something
55 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the
56 // number of the particular child.
GenerateChildName(const std::string & base_name,int64_t signature,int64_t child_id)57 std::string GenerateChildName(const std::string& base_name,
58 int64_t signature,
59 int64_t child_id) {
60 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(),
61 signature, child_id);
62 }
63
64 // This class deletes the children of a sparse entry.
65 class ChildrenDeleter
66 : public base::RefCounted<ChildrenDeleter>,
67 public disk_cache::FileIOCallback {
68 public:
ChildrenDeleter(disk_cache::BackendImpl * backend,const std::string & name)69 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name)
70 : backend_(backend->GetWeakPtr()), name_(name) {}
71
72 ChildrenDeleter(const ChildrenDeleter&) = delete;
73 ChildrenDeleter& operator=(const ChildrenDeleter&) = delete;
74
75 void OnFileIOComplete(int bytes_copied) override;
76
77 // Two ways of deleting the children: if we have the children map, use Start()
78 // directly, otherwise pass the data address to ReadData().
79 void Start(std::unique_ptr<char[]> buffer, int len);
80 void ReadData(disk_cache::Addr address, int len);
81
82 private:
83 friend class base::RefCounted<ChildrenDeleter>;
84 ~ChildrenDeleter() override = default;
85
86 void DeleteChildren();
87
88 base::WeakPtr<disk_cache::BackendImpl> backend_;
89 std::string name_;
90 disk_cache::Bitmap children_map_;
91 int64_t signature_ = 0;
92 std::unique_ptr<char[]> buffer_;
93 };
94
95 // This is the callback of the file operation.
OnFileIOComplete(int bytes_copied)96 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) {
97 Start(std::move(buffer_), bytes_copied);
98 }
99
Start(std::unique_ptr<char[]> buffer,int len)100 void ChildrenDeleter::Start(std::unique_ptr<char[]> buffer, int len) {
101 buffer_ = std::move(buffer);
102 if (len < static_cast<int>(sizeof(disk_cache::SparseData)))
103 return Release();
104
105 // Just copy the information from |buffer|, delete |buffer| and start deleting
106 // the child entries.
107 disk_cache::SparseData* data =
108 reinterpret_cast<disk_cache::SparseData*>(buffer_.get());
109 signature_ = data->header.signature;
110
111 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8;
112 children_map_.Resize(num_bits, false);
113 children_map_.SetMap(data->bitmap, num_bits / 32);
114 buffer_.reset();
115
116 DeleteChildren();
117 }
118
ReadData(disk_cache::Addr address,int len)119 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) {
120 DCHECK(address.is_block_file());
121 if (!backend_.get())
122 return Release();
123
124 disk_cache::File* file(backend_->File(address));
125 if (!file)
126 return Release();
127
128 size_t file_offset = address.start_block() * address.BlockSize() +
129 disk_cache::kBlockHeaderSize;
130
131 buffer_ = std::make_unique<char[]>(len);
132 bool completed;
133 if (!file->Read(buffer_.get(), len, file_offset, this, &completed))
134 return Release();
135
136 if (completed)
137 OnFileIOComplete(len);
138
139 // And wait until OnFileIOComplete gets called.
140 }
141
DeleteChildren()142 void ChildrenDeleter::DeleteChildren() {
143 int child_id = 0;
144 if (!children_map_.FindNextSetBit(&child_id) || !backend_.get()) {
145 // We are done. Just delete this object.
146 return Release();
147 }
148 std::string child_name = GenerateChildName(name_, signature_, child_id);
149 backend_->SyncDoomEntry(child_name);
150 children_map_.Set(child_id, false);
151
152 // Post a task to delete the next child.
153 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
154 FROM_HERE, base::BindOnce(&ChildrenDeleter::DeleteChildren, this));
155 }
156
157 // Returns the NetLog event type corresponding to a SparseOperation.
GetSparseEventType(disk_cache::SparseControl::SparseOperation operation)158 net::NetLogEventType GetSparseEventType(
159 disk_cache::SparseControl::SparseOperation operation) {
160 switch (operation) {
161 case disk_cache::SparseControl::kReadOperation:
162 return net::NetLogEventType::SPARSE_READ;
163 case disk_cache::SparseControl::kWriteOperation:
164 return net::NetLogEventType::SPARSE_WRITE;
165 case disk_cache::SparseControl::kGetRangeOperation:
166 return net::NetLogEventType::SPARSE_GET_RANGE;
167 default:
168 NOTREACHED();
169 return net::NetLogEventType::CANCELLED;
170 }
171 }
172
173 // Logs the end event for |operation| on a child entry. Range operations log
174 // no events for each child they search through.
LogChildOperationEnd(const net::NetLogWithSource & net_log,disk_cache::SparseControl::SparseOperation operation,int result)175 void LogChildOperationEnd(const net::NetLogWithSource& net_log,
176 disk_cache::SparseControl::SparseOperation operation,
177 int result) {
178 if (net_log.IsCapturing()) {
179 net::NetLogEventType event_type;
180 switch (operation) {
181 case disk_cache::SparseControl::kReadOperation:
182 event_type = net::NetLogEventType::SPARSE_READ_CHILD_DATA;
183 break;
184 case disk_cache::SparseControl::kWriteOperation:
185 event_type = net::NetLogEventType::SPARSE_WRITE_CHILD_DATA;
186 break;
187 case disk_cache::SparseControl::kGetRangeOperation:
188 return;
189 default:
190 NOTREACHED();
191 return;
192 }
193 net_log.EndEventWithNetErrorCode(event_type, result);
194 }
195 }
196
197 } // namespace.
198
199 namespace disk_cache {
200
SparseControl(EntryImpl * entry)201 SparseControl::SparseControl(EntryImpl* entry)
202 : entry_(entry),
203 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32) {
204 memset(&sparse_header_, 0, sizeof(sparse_header_));
205 memset(&child_data_, 0, sizeof(child_data_));
206 }
207
~SparseControl()208 SparseControl::~SparseControl() {
209 if (child_)
210 CloseChild();
211 if (init_)
212 WriteSparseData();
213 }
214
Init()215 int SparseControl::Init() {
216 DCHECK(!init_);
217
218 // We should not have sparse data for the exposed entry.
219 if (entry_->GetDataSize(kSparseData))
220 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
221
222 // Now see if there is something where we store our data.
223 int rv = net::OK;
224 int data_len = entry_->GetDataSize(kSparseIndex);
225 if (!data_len) {
226 rv = CreateSparseEntry();
227 } else {
228 rv = OpenSparseEntry(data_len);
229 }
230
231 if (rv == net::OK)
232 init_ = true;
233 return rv;
234 }
235
CouldBeSparse() const236 bool SparseControl::CouldBeSparse() const {
237 DCHECK(!init_);
238
239 if (entry_->GetDataSize(kSparseData))
240 return false;
241
242 // We don't verify the data, just see if it could be there.
243 return (entry_->GetDataSize(kSparseIndex) != 0);
244 }
245
StartIO(SparseOperation op,int64_t offset,net::IOBuffer * buf,int buf_len,CompletionOnceCallback callback)246 int SparseControl::StartIO(SparseOperation op,
247 int64_t offset,
248 net::IOBuffer* buf,
249 int buf_len,
250 CompletionOnceCallback callback) {
251 DCHECK(init_);
252 // We don't support simultaneous IO for sparse data.
253 if (operation_ != kNoOperation)
254 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
255
256 if (offset < 0 || buf_len < 0)
257 return net::ERR_INVALID_ARGUMENT;
258
259 int64_t end_offset = 0; // non-inclusive.
260 if (!base::CheckAdd(offset, buf_len).AssignIfValid(&end_offset)) {
261 // Writes aren't permitted to try to cross the end of address space;
262 // read/GetAvailableRange clip.
263 if (op == kWriteOperation)
264 return net::ERR_INVALID_ARGUMENT;
265 else
266 end_offset = std::numeric_limits<int64_t>::max();
267 }
268
269 if (offset >= kMaxEndOffset) {
270 // Interval is within valid offset space, but completely outside backend
271 // supported range. Permit GetAvailableRange to say "nothing here", actual
272 // I/O fails.
273 if (op == kGetRangeOperation)
274 return 0;
275 else
276 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
277 }
278
279 if (end_offset > kMaxEndOffset) {
280 // Interval is partially what the backend can handle. Fail writes, clip
281 // reads.
282 if (op == kWriteOperation)
283 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
284 else
285 end_offset = kMaxEndOffset;
286 }
287
288 DCHECK_GE(end_offset, offset);
289 buf_len = end_offset - offset;
290
291 DCHECK(!user_buf_.get());
292 DCHECK(user_callback_.is_null());
293
294 if (!buf && (op == kReadOperation || op == kWriteOperation))
295 return 0;
296
297 // Copy the operation parameters.
298 operation_ = op;
299 offset_ = offset;
300 user_buf_ = buf ? base::MakeRefCounted<net::DrainableIOBuffer>(buf, buf_len)
301 : nullptr;
302 buf_len_ = buf_len;
303 user_callback_ = std::move(callback);
304
305 result_ = 0;
306 pending_ = false;
307 finished_ = false;
308 abort_ = false;
309
310 if (entry_->net_log().IsCapturing()) {
311 NetLogSparseOperation(entry_->net_log(), GetSparseEventType(operation_),
312 net::NetLogEventPhase::BEGIN, offset_, buf_len_);
313 }
314 DoChildrenIO();
315
316 if (!pending_) {
317 // Everything was done synchronously.
318 operation_ = kNoOperation;
319 user_buf_ = nullptr;
320 user_callback_.Reset();
321 return result_;
322 }
323
324 return net::ERR_IO_PENDING;
325 }
326
GetAvailableRange(int64_t offset,int len)327 RangeResult SparseControl::GetAvailableRange(int64_t offset, int len) {
328 DCHECK(init_);
329 // We don't support simultaneous IO for sparse data.
330 if (operation_ != kNoOperation)
331 return RangeResult(net::ERR_CACHE_OPERATION_NOT_SUPPORTED);
332
333 range_found_ = false;
334 int result = StartIO(kGetRangeOperation, offset, nullptr, len,
335 CompletionOnceCallback());
336 if (range_found_)
337 return RangeResult(offset_, result);
338
339 // This is a failure. We want to return a valid start value if it's just an
340 // empty range, though.
341 if (result < 0)
342 return RangeResult(static_cast<net::Error>(result));
343 return RangeResult(offset, 0);
344 }
345
CancelIO()346 void SparseControl::CancelIO() {
347 if (operation_ == kNoOperation)
348 return;
349 abort_ = true;
350 }
351
ReadyToUse(CompletionOnceCallback callback)352 int SparseControl::ReadyToUse(CompletionOnceCallback callback) {
353 if (!abort_)
354 return net::OK;
355
356 // We'll grab another reference to keep this object alive because we just have
357 // one extra reference due to the pending IO operation itself, but we'll
358 // release that one before invoking user_callback_.
359 entry_->AddRef(); // Balanced in DoAbortCallbacks.
360 abort_callbacks_.push_back(std::move(callback));
361 return net::ERR_IO_PENDING;
362 }
363
364 // Static
DeleteChildren(EntryImpl * entry)365 void SparseControl::DeleteChildren(EntryImpl* entry) {
366 DCHECK(entry->GetEntryFlags() & PARENT_ENTRY);
367 int data_len = entry->GetDataSize(kSparseIndex);
368 if (data_len < static_cast<int>(sizeof(SparseData)) ||
369 entry->GetDataSize(kSparseData))
370 return;
371
372 int map_len = data_len - sizeof(SparseHeader);
373 if (map_len > kMaxMapSize || map_len % 4)
374 return;
375
376 std::unique_ptr<char[]> buffer;
377 Addr address;
378 entry->GetData(kSparseIndex, &buffer, &address);
379 if (!buffer && !address.is_initialized())
380 return;
381
382 entry->net_log().AddEvent(net::NetLogEventType::SPARSE_DELETE_CHILDREN);
383
384 DCHECK(entry->backend_.get());
385 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(),
386 entry->GetKey());
387 // The object will self destruct when finished.
388 deleter->AddRef();
389
390 if (buffer) {
391 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
392 FROM_HERE, base::BindOnce(&ChildrenDeleter::Start, deleter,
393 std::move(buffer), data_len));
394 } else {
395 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
396 FROM_HERE,
397 base::BindOnce(&ChildrenDeleter::ReadData, deleter, address, data_len));
398 }
399 }
400
401 // We are going to start using this entry to store sparse data, so we have to
402 // initialize our control info.
CreateSparseEntry()403 int SparseControl::CreateSparseEntry() {
404 if (CHILD_ENTRY & entry_->GetEntryFlags())
405 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
406
407 memset(&sparse_header_, 0, sizeof(sparse_header_));
408 sparse_header_.signature = Time::Now().ToInternalValue();
409 sparse_header_.magic = kIndexMagic;
410 sparse_header_.parent_key_len = entry_->GetKey().size();
411 children_map_.Resize(kNumSparseBits, true);
412
413 // Save the header. The bitmap is saved in the destructor.
414 scoped_refptr<net::IOBuffer> buf = base::MakeRefCounted<net::WrappedIOBuffer>(
415 reinterpret_cast<char*>(&sparse_header_));
416
417 int rv = entry_->WriteData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_),
418 CompletionOnceCallback(), false);
419 if (rv != sizeof(sparse_header_)) {
420 DLOG(ERROR) << "Unable to save sparse_header_";
421 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
422 }
423
424 entry_->SetEntryFlags(PARENT_ENTRY);
425 return net::OK;
426 }
427
428 // We are opening an entry from disk. Make sure that our control data is there.
OpenSparseEntry(int data_len)429 int SparseControl::OpenSparseEntry(int data_len) {
430 if (data_len < static_cast<int>(sizeof(SparseData)))
431 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
432
433 if (entry_->GetDataSize(kSparseData))
434 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
435
436 if (!(PARENT_ENTRY & entry_->GetEntryFlags()))
437 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
438
439 // Don't go over board with the bitmap.
440 int map_len = data_len - sizeof(sparse_header_);
441 if (map_len > kMaxMapSize || map_len % 4)
442 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
443
444 scoped_refptr<net::IOBuffer> buf = base::MakeRefCounted<net::WrappedIOBuffer>(
445 reinterpret_cast<char*>(&sparse_header_));
446
447 // Read header.
448 int rv = entry_->ReadData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_),
449 CompletionOnceCallback());
450 if (rv != static_cast<int>(sizeof(sparse_header_)))
451 return net::ERR_CACHE_READ_FAILURE;
452
453 // The real validation should be performed by the caller. This is just to
454 // double check.
455 if (sparse_header_.magic != kIndexMagic ||
456 sparse_header_.parent_key_len !=
457 static_cast<int>(entry_->GetKey().size()))
458 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
459
460 // Read the actual bitmap.
461 buf = base::MakeRefCounted<net::IOBuffer>(map_len);
462 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(),
463 map_len, CompletionOnceCallback());
464 if (rv != map_len)
465 return net::ERR_CACHE_READ_FAILURE;
466
467 // Grow the bitmap to the current size and copy the bits.
468 children_map_.Resize(map_len * 8, false);
469 children_map_.SetMap(reinterpret_cast<uint32_t*>(buf->data()), map_len);
470 return net::OK;
471 }
472
OpenChild()473 bool SparseControl::OpenChild() {
474 DCHECK_GE(result_, 0);
475
476 std::string key = GenerateChildKey();
477 if (child_) {
478 // Keep using the same child or open another one?.
479 if (key == child_->GetKey())
480 return true;
481 CloseChild();
482 }
483
484 // See if we are tracking this child.
485 if (!ChildPresent())
486 return ContinueWithoutChild(key);
487
488 if (!entry_->backend_.get())
489 return false;
490
491 child_ = entry_->backend_->OpenEntryImpl(key);
492 if (!child_)
493 return ContinueWithoutChild(key);
494
495 if (!(CHILD_ENTRY & child_->GetEntryFlags()) ||
496 child_->GetDataSize(kSparseIndex) < static_cast<int>(sizeof(child_data_)))
497 return KillChildAndContinue(key, false);
498
499 scoped_refptr<net::WrappedIOBuffer> buf =
500 base::MakeRefCounted<net::WrappedIOBuffer>(
501 reinterpret_cast<char*>(&child_data_));
502
503 // Read signature.
504 int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
505 CompletionOnceCallback());
506 if (rv != sizeof(child_data_))
507 return KillChildAndContinue(key, true); // This is a fatal failure.
508
509 if (child_data_.header.signature != sparse_header_.signature ||
510 child_data_.header.magic != kIndexMagic)
511 return KillChildAndContinue(key, false);
512
513 if (child_data_.header.last_block_len < 0 ||
514 child_data_.header.last_block_len >= kBlockSize) {
515 // Make sure these values are always within range.
516 child_data_.header.last_block_len = 0;
517 child_data_.header.last_block = -1;
518 }
519
520 return true;
521 }
522
CloseChild()523 void SparseControl::CloseChild() {
524 scoped_refptr<net::WrappedIOBuffer> buf =
525 base::MakeRefCounted<net::WrappedIOBuffer>(
526 reinterpret_cast<char*>(&child_data_));
527
528 // Save the allocation bitmap before closing the child entry.
529 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
530 CompletionOnceCallback(), false);
531 if (rv != sizeof(child_data_))
532 DLOG(ERROR) << "Failed to save child data";
533 child_ = nullptr;
534 }
535
GenerateChildKey()536 std::string SparseControl::GenerateChildKey() {
537 return GenerateChildName(entry_->GetKey(), sparse_header_.signature,
538 offset_ >> 20);
539 }
540
541 // We are deleting the child because something went wrong.
KillChildAndContinue(const std::string & key,bool fatal)542 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) {
543 SetChildBit(false);
544 child_->DoomImpl();
545 child_ = nullptr;
546 if (fatal) {
547 result_ = net::ERR_CACHE_READ_FAILURE;
548 return false;
549 }
550 return ContinueWithoutChild(key);
551 }
552
553 // We were not able to open this child; see what we can do.
ContinueWithoutChild(const std::string & key)554 bool SparseControl::ContinueWithoutChild(const std::string& key) {
555 if (kReadOperation == operation_)
556 return false;
557 if (kGetRangeOperation == operation_)
558 return true;
559
560 if (!entry_->backend_.get())
561 return false;
562
563 child_ = entry_->backend_->CreateEntryImpl(key);
564 if (!child_) {
565 child_ = nullptr;
566 result_ = net::ERR_CACHE_READ_FAILURE;
567 return false;
568 }
569 // Write signature.
570 InitChildData();
571 return true;
572 }
573
ChildPresent()574 bool SparseControl::ChildPresent() {
575 int child_bit = static_cast<int>(offset_ >> 20);
576 if (children_map_.Size() <= child_bit)
577 return false;
578
579 return children_map_.Get(child_bit);
580 }
581
SetChildBit(bool value)582 void SparseControl::SetChildBit(bool value) {
583 int child_bit = static_cast<int>(offset_ >> 20);
584
585 // We may have to increase the bitmap of child entries.
586 if (children_map_.Size() <= child_bit)
587 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true);
588
589 children_map_.Set(child_bit, value);
590 }
591
WriteSparseData()592 void SparseControl::WriteSparseData() {
593 scoped_refptr<net::IOBuffer> buf = base::MakeRefCounted<net::WrappedIOBuffer>(
594 reinterpret_cast<const char*>(children_map_.GetMap()));
595
596 int len = children_map_.ArraySize() * 4;
597 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(),
598 len, CompletionOnceCallback(), false);
599 if (rv != len) {
600 DLOG(ERROR) << "Unable to save sparse map";
601 }
602 }
603
VerifyRange()604 bool SparseControl::VerifyRange() {
605 DCHECK_GE(result_, 0);
606
607 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1);
608 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_);
609
610 // We can write to (or get info from) anywhere in this child.
611 if (operation_ != kReadOperation)
612 return true;
613
614 // Check that there are no holes in this range.
615 int last_bit = (child_offset_ + child_len_ + 1023) >> 10;
616 int start = child_offset_ >> 10;
617 if (child_map_.FindNextBit(&start, last_bit, false)) {
618 // Something is not here.
619 DCHECK_GE(child_data_.header.last_block_len, 0);
620 DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
621 int partial_block_len = PartialBlockLength(start);
622 if (start == child_offset_ >> 10) {
623 // It looks like we don't have anything.
624 if (partial_block_len <= (child_offset_ & (kBlockSize - 1)))
625 return false;
626 }
627
628 // We have the first part.
629 child_len_ = (start << 10) - child_offset_;
630 if (partial_block_len) {
631 // We may have a few extra bytes.
632 child_len_ = std::min(child_len_ + partial_block_len, buf_len_);
633 }
634 // There is no need to read more after this one.
635 buf_len_ = child_len_;
636 }
637 return true;
638 }
639
UpdateRange(int result)640 void SparseControl::UpdateRange(int result) {
641 if (result <= 0 || operation_ != kWriteOperation)
642 return;
643
644 DCHECK_GE(child_data_.header.last_block_len, 0);
645 DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
646
647 // Write the bitmap.
648 int first_bit = child_offset_ >> 10;
649 int block_offset = child_offset_ & (kBlockSize - 1);
650 if (block_offset && (child_data_.header.last_block != first_bit ||
651 child_data_.header.last_block_len < block_offset)) {
652 // The first block is not completely filled; ignore it.
653 first_bit++;
654 }
655
656 int last_bit = (child_offset_ + result) >> 10;
657 block_offset = (child_offset_ + result) & (kBlockSize - 1);
658
659 // This condition will hit with the following criteria:
660 // 1. The first byte doesn't follow the last write.
661 // 2. The first byte is in the middle of a block.
662 // 3. The first byte and the last byte are in the same block.
663 if (first_bit > last_bit)
664 return;
665
666 if (block_offset && !child_map_.Get(last_bit)) {
667 // The last block is not completely filled; save it for later.
668 child_data_.header.last_block = last_bit;
669 child_data_.header.last_block_len = block_offset;
670 } else {
671 child_data_.header.last_block = -1;
672 }
673
674 child_map_.SetRange(first_bit, last_bit, true);
675 }
676
PartialBlockLength(int block_index) const677 int SparseControl::PartialBlockLength(int block_index) const {
678 if (block_index == child_data_.header.last_block)
679 return child_data_.header.last_block_len;
680
681 // This is really empty.
682 return 0;
683 }
684
InitChildData()685 void SparseControl::InitChildData() {
686 child_->SetEntryFlags(CHILD_ENTRY);
687
688 memset(&child_data_, 0, sizeof(child_data_));
689 child_data_.header = sparse_header_;
690
691 scoped_refptr<net::WrappedIOBuffer> buf =
692 base::MakeRefCounted<net::WrappedIOBuffer>(
693 reinterpret_cast<char*>(&child_data_));
694
695 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
696 CompletionOnceCallback(), false);
697 if (rv != sizeof(child_data_))
698 DLOG(ERROR) << "Failed to save child data";
699 SetChildBit(true);
700 }
701
DoChildrenIO()702 void SparseControl::DoChildrenIO() {
703 while (DoChildIO()) continue;
704
705 // Range operations are finished synchronously, often without setting
706 // |finished_| to true.
707 if (kGetRangeOperation == operation_ && entry_->net_log().IsCapturing()) {
708 entry_->net_log().EndEvent(net::NetLogEventType::SPARSE_GET_RANGE, [&] {
709 return CreateNetLogGetAvailableRangeResultParams(
710 RangeResult(offset_, result_));
711 });
712 }
713 if (finished_) {
714 if (kGetRangeOperation != operation_ && entry_->net_log().IsCapturing()) {
715 entry_->net_log().EndEvent(GetSparseEventType(operation_));
716 }
717 if (pending_)
718 DoUserCallback(); // Don't touch this object after this point.
719 }
720 }
721
DoChildIO()722 bool SparseControl::DoChildIO() {
723 finished_ = true;
724 if (!buf_len_ || result_ < 0)
725 return false;
726
727 if (!OpenChild())
728 return false;
729
730 if (!VerifyRange())
731 return false;
732
733 // We have more work to do. Let's not trigger a callback to the caller.
734 finished_ = false;
735 CompletionOnceCallback callback;
736 if (!user_callback_.is_null()) {
737 callback = base::BindOnce(&SparseControl::OnChildIOCompleted,
738 base::Unretained(this));
739 }
740
741 int rv = 0;
742 switch (operation_) {
743 case kReadOperation:
744 if (entry_->net_log().IsCapturing()) {
745 NetLogSparseReadWrite(entry_->net_log(),
746 net::NetLogEventType::SPARSE_READ_CHILD_DATA,
747 net::NetLogEventPhase::BEGIN,
748 child_->net_log().source(), child_len_);
749 }
750 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(),
751 child_len_, std::move(callback));
752 break;
753 case kWriteOperation:
754 if (entry_->net_log().IsCapturing()) {
755 NetLogSparseReadWrite(entry_->net_log(),
756 net::NetLogEventType::SPARSE_WRITE_CHILD_DATA,
757 net::NetLogEventPhase::BEGIN,
758 child_->net_log().source(), child_len_);
759 }
760 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(),
761 child_len_, std::move(callback), false);
762 break;
763 case kGetRangeOperation:
764 rv = DoGetAvailableRange();
765 break;
766 default:
767 NOTREACHED();
768 }
769
770 if (rv == net::ERR_IO_PENDING) {
771 if (!pending_) {
772 pending_ = true;
773 // The child will protect himself against closing the entry while IO is in
774 // progress. However, this entry can still be closed, and that would not
775 // be a good thing for us, so we increase the refcount until we're
776 // finished doing sparse stuff.
777 entry_->AddRef(); // Balanced in DoUserCallback.
778 }
779 return false;
780 }
781 if (!rv)
782 return false;
783
784 DoChildIOCompleted(rv);
785 return true;
786 }
787
DoGetAvailableRange()788 int SparseControl::DoGetAvailableRange() {
789 if (!child_)
790 return child_len_; // Move on to the next child.
791
792 // Blockfile splits sparse files into multiple child entries, each responsible
793 // for managing 1MiB of address space. This method is responsible for
794 // implementing GetAvailableRange within a single child.
795 //
796 // Input:
797 // |child_offset_|, |child_len_|:
798 // describe range in current child's address space the client requested.
799 // |offset_| is equivalent to |child_offset_| but in global address space.
800 //
801 // For example if this were child [2] and the original call was for
802 // [0x200005, 0x200007) then |offset_| would be 0x200005, |child_offset_|
803 // would be 5, and |child_len| would be 2.
804 //
805 // Output:
806 // If nothing found:
807 // return |child_len_|
808 //
809 // If something found:
810 // |result_| gets the length of the available range.
811 // |offset_| gets the global address of beginning of the available range.
812 // |range_found_| get true to signal SparseControl::GetAvailableRange().
813 // return 0 to exit loop.
814 net::Interval<int> to_find(child_offset_, child_offset_ + child_len_);
815
816 // Within each child, valid portions are mostly tracked via the |child_map_|
817 // bitmap which marks which 1KiB 'blocks' have valid data. Scan the bitmap
818 // for the first contiguous range of set bits that's relevant to the range
819 // [child_offset_, child_offset_ + len)
820 int first_bit = child_offset_ >> 10;
821 int last_bit = (child_offset_ + child_len_ + kBlockSize - 1) >> 10;
822 int found = first_bit;
823 int bits_found = child_map_.FindBits(&found, last_bit, true);
824 net::Interval<int> bitmap_range(found * kBlockSize,
825 found * kBlockSize + bits_found * kBlockSize);
826
827 // Bits on the bitmap should only be set when the corresponding block was
828 // fully written (it's really being used). If a block is partially used, it
829 // has to start with valid data, the length of the valid data is saved in
830 // |header.last_block_len| and the block number saved in |header.last_block|.
831 // This is updated after every write; with |header.last_block| set to -1
832 // if no sub-KiB range is being tracked.
833 net::Interval<int> last_write_range;
834 if (child_data_.header.last_block >= 0) {
835 last_write_range =
836 net::Interval<int>(child_data_.header.last_block * kBlockSize,
837 child_data_.header.last_block * kBlockSize +
838 child_data_.header.last_block_len);
839 }
840
841 // Often |last_write_range| is contiguously after |bitmap_range|, but not
842 // always. See if they can be combined.
843 if (!last_write_range.Empty() && !bitmap_range.Empty() &&
844 bitmap_range.max() == last_write_range.min()) {
845 bitmap_range.SetMax(last_write_range.max());
846 last_write_range.Clear();
847 }
848
849 // Do any of them have anything relevant?
850 bitmap_range.IntersectWith(to_find);
851 last_write_range.IntersectWith(to_find);
852
853 // Now return the earliest non-empty interval, if any.
854 net::Interval<int> result_range = bitmap_range;
855 if (bitmap_range.Empty() || (!last_write_range.Empty() &&
856 last_write_range.min() < bitmap_range.min()))
857 result_range = last_write_range;
858
859 if (result_range.Empty()) {
860 // Nothing found, so we just skip over this child.
861 return child_len_;
862 }
863
864 // Package up our results.
865 range_found_ = true;
866 offset_ += result_range.min() - child_offset_;
867 result_ = result_range.max() - result_range.min();
868 return 0;
869 }
870
DoChildIOCompleted(int result)871 void SparseControl::DoChildIOCompleted(int result) {
872 LogChildOperationEnd(entry_->net_log(), operation_, result);
873 if (result < 0) {
874 // We fail the whole operation if we encounter an error.
875 result_ = result;
876 return;
877 }
878
879 UpdateRange(result);
880
881 result_ += result;
882 offset_ += result;
883 buf_len_ -= result;
884
885 // We'll be reusing the user provided buffer for the next chunk.
886 if (buf_len_ && user_buf_.get())
887 user_buf_->DidConsume(result);
888 }
889
OnChildIOCompleted(int result)890 void SparseControl::OnChildIOCompleted(int result) {
891 DCHECK_NE(net::ERR_IO_PENDING, result);
892 DoChildIOCompleted(result);
893
894 if (abort_) {
895 // We'll return the current result of the operation, which may be less than
896 // the bytes to read or write, but the user cancelled the operation.
897 abort_ = false;
898 if (entry_->net_log().IsCapturing()) {
899 entry_->net_log().AddEvent(net::NetLogEventType::CANCELLED);
900 entry_->net_log().EndEvent(GetSparseEventType(operation_));
901 }
902 // We have an indirect reference to this object for every callback so if
903 // there is only one callback, we may delete this object before reaching
904 // DoAbortCallbacks.
905 bool has_abort_callbacks = !abort_callbacks_.empty();
906 DoUserCallback();
907 if (has_abort_callbacks)
908 DoAbortCallbacks();
909 return;
910 }
911
912 // We are running a callback from the message loop. It's time to restart what
913 // we were doing before.
914 DoChildrenIO();
915 }
916
DoUserCallback()917 void SparseControl::DoUserCallback() {
918 DCHECK(!user_callback_.is_null());
919 CompletionOnceCallback cb = std::move(user_callback_);
920 user_buf_ = nullptr;
921 pending_ = false;
922 operation_ = kNoOperation;
923 int rv = result_;
924 entry_->Release(); // Don't touch object after this line.
925 std::move(cb).Run(rv);
926 }
927
DoAbortCallbacks()928 void SparseControl::DoAbortCallbacks() {
929 std::vector<CompletionOnceCallback> abort_callbacks;
930 abort_callbacks.swap(abort_callbacks_);
931
932 for (CompletionOnceCallback& callback : abort_callbacks) {
933 // Releasing all references to entry_ may result in the destruction of this
934 // object so we should not be touching it after the last Release().
935 entry_->Release();
936 std::move(callback).Run(net::OK);
937 }
938 }
939
940 } // namespace disk_cache
941