xref: /aosp_15_r20/external/cronet/net/disk_cache/blockfile/sparse_control.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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       base::as_chars(base::make_span(&sparse_header_, 1u)));
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       base::as_chars(base::span(&sparse_header_, 1u)));
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::IOBufferWithSize>(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   auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
500       base::as_chars(base::make_span(&child_data_, 1u)));
501 
502   // Read signature.
503   int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
504                             CompletionOnceCallback());
505   if (rv != sizeof(child_data_))
506     return KillChildAndContinue(key, true);  // This is a fatal failure.
507 
508   if (child_data_.header.signature != sparse_header_.signature ||
509       child_data_.header.magic != kIndexMagic)
510     return KillChildAndContinue(key, false);
511 
512   if (child_data_.header.last_block_len < 0 ||
513       child_data_.header.last_block_len >= kBlockSize) {
514     // Make sure these values are always within range.
515     child_data_.header.last_block_len = 0;
516     child_data_.header.last_block = -1;
517   }
518 
519   return true;
520 }
521 
CloseChild()522 void SparseControl::CloseChild() {
523   auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
524       base::as_chars(base::make_span(&child_data_, 1u)));
525 
526   // Save the allocation bitmap before closing the child entry.
527   int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
528                              CompletionOnceCallback(), false);
529   if (rv != sizeof(child_data_))
530     DLOG(ERROR) << "Failed to save child data";
531   child_ = nullptr;
532 }
533 
GenerateChildKey()534 std::string SparseControl::GenerateChildKey() {
535   return GenerateChildName(entry_->GetKey(), sparse_header_.signature,
536                            offset_ >> 20);
537 }
538 
539 // We are deleting the child because something went wrong.
KillChildAndContinue(const std::string & key,bool fatal)540 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) {
541   SetChildBit(false);
542   child_->DoomImpl();
543   child_ = nullptr;
544   if (fatal) {
545     result_ = net::ERR_CACHE_READ_FAILURE;
546     return false;
547   }
548   return ContinueWithoutChild(key);
549 }
550 
551 // We were not able to open this child; see what we can do.
ContinueWithoutChild(const std::string & key)552 bool SparseControl::ContinueWithoutChild(const std::string& key) {
553   if (kReadOperation == operation_)
554     return false;
555   if (kGetRangeOperation == operation_)
556     return true;
557 
558   if (!entry_->backend_.get())
559     return false;
560 
561   child_ = entry_->backend_->CreateEntryImpl(key);
562   if (!child_) {
563     child_ = nullptr;
564     result_ = net::ERR_CACHE_READ_FAILURE;
565     return false;
566   }
567   // Write signature.
568   InitChildData();
569   return true;
570 }
571 
ChildPresent()572 bool SparseControl::ChildPresent() {
573   int child_bit = static_cast<int>(offset_ >> 20);
574   if (children_map_.Size() <= child_bit)
575     return false;
576 
577   return children_map_.Get(child_bit);
578 }
579 
SetChildBit(bool value)580 void SparseControl::SetChildBit(bool value) {
581   int child_bit = static_cast<int>(offset_ >> 20);
582 
583   // We may have to increase the bitmap of child entries.
584   if (children_map_.Size() <= child_bit)
585     children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true);
586 
587   children_map_.Set(child_bit, value);
588 }
589 
WriteSparseData()590 void SparseControl::WriteSparseData() {
591   auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
592       base::as_chars(children_map_.GetSpan()));
593 
594   int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(),
595                              buf->size(), CompletionOnceCallback(), false);
596   if (rv != buf->size()) {
597     DLOG(ERROR) << "Unable to save sparse map";
598   }
599 }
600 
VerifyRange()601 bool SparseControl::VerifyRange() {
602   DCHECK_GE(result_, 0);
603 
604   child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1);
605   child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_);
606 
607   // We can write to (or get info from) anywhere in this child.
608   if (operation_ != kReadOperation)
609     return true;
610 
611   // Check that there are no holes in this range.
612   int last_bit = (child_offset_ + child_len_ + 1023) >> 10;
613   int start = child_offset_ >> 10;
614   if (child_map_.FindNextBit(&start, last_bit, false)) {
615     // Something is not here.
616     DCHECK_GE(child_data_.header.last_block_len, 0);
617     DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
618     int partial_block_len = PartialBlockLength(start);
619     if (start == child_offset_ >> 10) {
620       // It looks like we don't have anything.
621       if (partial_block_len <= (child_offset_ & (kBlockSize - 1)))
622         return false;
623     }
624 
625     // We have the first part.
626     child_len_ = (start << 10) - child_offset_;
627     if (partial_block_len) {
628       // We may have a few extra bytes.
629       child_len_ = std::min(child_len_ + partial_block_len, buf_len_);
630     }
631     // There is no need to read more after this one.
632     buf_len_ = child_len_;
633   }
634   return true;
635 }
636 
UpdateRange(int result)637 void SparseControl::UpdateRange(int result) {
638   if (result <= 0 || operation_ != kWriteOperation)
639     return;
640 
641   DCHECK_GE(child_data_.header.last_block_len, 0);
642   DCHECK_LT(child_data_.header.last_block_len, kBlockSize);
643 
644   // Write the bitmap.
645   int first_bit = child_offset_ >> 10;
646   int block_offset = child_offset_ & (kBlockSize - 1);
647   if (block_offset && (child_data_.header.last_block != first_bit ||
648                        child_data_.header.last_block_len < block_offset)) {
649     // The first block is not completely filled; ignore it.
650     first_bit++;
651   }
652 
653   int last_bit = (child_offset_ + result) >> 10;
654   block_offset = (child_offset_ + result) & (kBlockSize - 1);
655 
656   // This condition will hit with the following criteria:
657   // 1. The first byte doesn't follow the last write.
658   // 2. The first byte is in the middle of a block.
659   // 3. The first byte and the last byte are in the same block.
660   if (first_bit > last_bit)
661     return;
662 
663   if (block_offset && !child_map_.Get(last_bit)) {
664     // The last block is not completely filled; save it for later.
665     child_data_.header.last_block = last_bit;
666     child_data_.header.last_block_len = block_offset;
667   } else {
668     child_data_.header.last_block = -1;
669   }
670 
671   child_map_.SetRange(first_bit, last_bit, true);
672 }
673 
PartialBlockLength(int block_index) const674 int SparseControl::PartialBlockLength(int block_index) const {
675   if (block_index == child_data_.header.last_block)
676     return child_data_.header.last_block_len;
677 
678   // This is really empty.
679   return 0;
680 }
681 
InitChildData()682 void SparseControl::InitChildData() {
683   child_->SetEntryFlags(CHILD_ENTRY);
684 
685   memset(&child_data_, 0, sizeof(child_data_));
686   child_data_.header = sparse_header_;
687 
688   auto buf = base::MakeRefCounted<net::WrappedIOBuffer>(
689       base::as_chars(base::make_span(&child_data_, 1u)));
690 
691   int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_),
692                              CompletionOnceCallback(), false);
693   if (rv != sizeof(child_data_))
694     DLOG(ERROR) << "Failed to save child data";
695   SetChildBit(true);
696 }
697 
DoChildrenIO()698 void SparseControl::DoChildrenIO() {
699   while (DoChildIO()) continue;
700 
701   // Range operations are finished synchronously, often without setting
702   // |finished_| to true.
703   if (kGetRangeOperation == operation_ && entry_->net_log().IsCapturing()) {
704     entry_->net_log().EndEvent(net::NetLogEventType::SPARSE_GET_RANGE, [&] {
705       return CreateNetLogGetAvailableRangeResultParams(
706           RangeResult(offset_, result_));
707     });
708   }
709   if (finished_) {
710     if (kGetRangeOperation != operation_ && entry_->net_log().IsCapturing()) {
711       entry_->net_log().EndEvent(GetSparseEventType(operation_));
712     }
713     if (pending_)
714       DoUserCallback();  // Don't touch this object after this point.
715   }
716 }
717 
DoChildIO()718 bool SparseControl::DoChildIO() {
719   finished_ = true;
720   if (!buf_len_ || result_ < 0)
721     return false;
722 
723   if (!OpenChild())
724     return false;
725 
726   if (!VerifyRange())
727     return false;
728 
729   // We have more work to do. Let's not trigger a callback to the caller.
730   finished_ = false;
731   CompletionOnceCallback callback;
732   if (!user_callback_.is_null()) {
733     callback = base::BindOnce(&SparseControl::OnChildIOCompleted,
734                               base::Unretained(this));
735   }
736 
737   int rv = 0;
738   switch (operation_) {
739     case kReadOperation:
740       if (entry_->net_log().IsCapturing()) {
741         NetLogSparseReadWrite(entry_->net_log(),
742                               net::NetLogEventType::SPARSE_READ_CHILD_DATA,
743                               net::NetLogEventPhase::BEGIN,
744                               child_->net_log().source(), child_len_);
745       }
746       rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(),
747                                 child_len_, std::move(callback));
748       break;
749     case kWriteOperation:
750       if (entry_->net_log().IsCapturing()) {
751         NetLogSparseReadWrite(entry_->net_log(),
752                               net::NetLogEventType::SPARSE_WRITE_CHILD_DATA,
753                               net::NetLogEventPhase::BEGIN,
754                               child_->net_log().source(), child_len_);
755       }
756       rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(),
757                                  child_len_, std::move(callback), false);
758       break;
759     case kGetRangeOperation:
760       rv = DoGetAvailableRange();
761       break;
762     default:
763       NOTREACHED();
764   }
765 
766   if (rv == net::ERR_IO_PENDING) {
767     if (!pending_) {
768       pending_ = true;
769       // The child will protect himself against closing the entry while IO is in
770       // progress. However, this entry can still be closed, and that would not
771       // be a good thing for us, so we increase the refcount until we're
772       // finished doing sparse stuff.
773       entry_->AddRef();  // Balanced in DoUserCallback.
774     }
775     return false;
776   }
777   if (!rv)
778     return false;
779 
780   DoChildIOCompleted(rv);
781   return true;
782 }
783 
DoGetAvailableRange()784 int SparseControl::DoGetAvailableRange() {
785   if (!child_)
786     return child_len_;  // Move on to the next child.
787 
788   // Blockfile splits sparse files into multiple child entries, each responsible
789   // for managing 1MiB of address space. This method is responsible for
790   // implementing GetAvailableRange within a single child.
791   //
792   // Input:
793   //   |child_offset_|, |child_len_|:
794   //     describe range in current child's address space the client requested.
795   //   |offset_| is equivalent to |child_offset_| but in global address space.
796   //
797   //   For example if this were child [2] and the original call was for
798   //   [0x200005, 0x200007) then |offset_| would be 0x200005, |child_offset_|
799   //   would be 5, and |child_len| would be 2.
800   //
801   // Output:
802   //   If nothing found:
803   //     return |child_len_|
804   //
805   //   If something found:
806   //     |result_| gets the length of the available range.
807   //     |offset_| gets the global address of beginning of the available range.
808   //     |range_found_| get true to signal SparseControl::GetAvailableRange().
809   //     return 0 to exit loop.
810   net::Interval<int> to_find(child_offset_, child_offset_ + child_len_);
811 
812   // Within each child, valid portions are mostly tracked via the |child_map_|
813   // bitmap which marks which 1KiB 'blocks' have valid data. Scan the bitmap
814   // for the first contiguous range of set bits that's relevant to the range
815   // [child_offset_, child_offset_ + len)
816   int first_bit = child_offset_ >> 10;
817   int last_bit = (child_offset_ + child_len_ + kBlockSize - 1) >> 10;
818   int found = first_bit;
819   int bits_found = child_map_.FindBits(&found, last_bit, true);
820   net::Interval<int> bitmap_range(found * kBlockSize,
821                                   found * kBlockSize + bits_found * kBlockSize);
822 
823   // Bits on the bitmap should only be set when the corresponding block was
824   // fully written (it's really being used). If a block is partially used, it
825   // has to start with valid data, the length of the valid data is saved in
826   // |header.last_block_len| and the block number saved in |header.last_block|.
827   // This is updated after every write; with |header.last_block| set to -1
828   // if no sub-KiB range is being tracked.
829   net::Interval<int> last_write_range;
830   if (child_data_.header.last_block >= 0) {
831     last_write_range =
832         net::Interval<int>(child_data_.header.last_block * kBlockSize,
833                            child_data_.header.last_block * kBlockSize +
834                                child_data_.header.last_block_len);
835   }
836 
837   // Often |last_write_range| is contiguously after |bitmap_range|, but not
838   // always. See if they can be combined.
839   if (!last_write_range.Empty() && !bitmap_range.Empty() &&
840       bitmap_range.max() == last_write_range.min()) {
841     bitmap_range.SetMax(last_write_range.max());
842     last_write_range.Clear();
843   }
844 
845   // Do any of them have anything relevant?
846   bitmap_range.IntersectWith(to_find);
847   last_write_range.IntersectWith(to_find);
848 
849   // Now return the earliest non-empty interval, if any.
850   net::Interval<int> result_range = bitmap_range;
851   if (bitmap_range.Empty() || (!last_write_range.Empty() &&
852                                last_write_range.min() < bitmap_range.min()))
853     result_range = last_write_range;
854 
855   if (result_range.Empty()) {
856     // Nothing found, so we just skip over this child.
857     return child_len_;
858   }
859 
860   // Package up our results.
861   range_found_ = true;
862   offset_ += result_range.min() - child_offset_;
863   result_ = result_range.max() - result_range.min();
864   return 0;
865 }
866 
DoChildIOCompleted(int result)867 void SparseControl::DoChildIOCompleted(int result) {
868   LogChildOperationEnd(entry_->net_log(), operation_, result);
869   if (result < 0) {
870     // We fail the whole operation if we encounter an error.
871     result_ = result;
872     return;
873   }
874 
875   UpdateRange(result);
876 
877   result_ += result;
878   offset_ += result;
879   buf_len_ -= result;
880 
881   // We'll be reusing the user provided buffer for the next chunk.
882   if (buf_len_ && user_buf_.get())
883     user_buf_->DidConsume(result);
884 }
885 
OnChildIOCompleted(int result)886 void SparseControl::OnChildIOCompleted(int result) {
887   DCHECK_NE(net::ERR_IO_PENDING, result);
888   DoChildIOCompleted(result);
889 
890   if (abort_) {
891     // We'll return the current result of the operation, which may be less than
892     // the bytes to read or write, but the user cancelled the operation.
893     abort_ = false;
894     if (entry_->net_log().IsCapturing()) {
895       entry_->net_log().AddEvent(net::NetLogEventType::CANCELLED);
896       entry_->net_log().EndEvent(GetSparseEventType(operation_));
897     }
898     // We have an indirect reference to this object for every callback so if
899     // there is only one callback, we may delete this object before reaching
900     // DoAbortCallbacks.
901     bool has_abort_callbacks = !abort_callbacks_.empty();
902     DoUserCallback();
903     if (has_abort_callbacks)
904       DoAbortCallbacks();
905     return;
906   }
907 
908   // We are running a callback from the message loop. It's time to restart what
909   // we were doing before.
910   DoChildrenIO();
911 }
912 
DoUserCallback()913 void SparseControl::DoUserCallback() {
914   DCHECK(!user_callback_.is_null());
915   CompletionOnceCallback cb = std::move(user_callback_);
916   user_buf_ = nullptr;
917   pending_ = false;
918   operation_ = kNoOperation;
919   int rv = result_;
920   entry_->Release();  // Don't touch object after this line.
921   std::move(cb).Run(rv);
922 }
923 
DoAbortCallbacks()924 void SparseControl::DoAbortCallbacks() {
925   std::vector<CompletionOnceCallback> abort_callbacks;
926   abort_callbacks.swap(abort_callbacks_);
927 
928   for (CompletionOnceCallback& callback : abort_callbacks) {
929     // Releasing all references to entry_ may result in the destruction of this
930     // object so we should not be touching it after the last Release().
931     entry_->Release();
932     std::move(callback).Run(net::OK);
933   }
934 }
935 
936 }  // namespace disk_cache
937