1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/sparse_control.h"
8 #include "base/format_macros.h"
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/time.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/disk_cache/backend_impl.h"
17 #include "net/disk_cache/entry_impl.h"
18 #include "net/disk_cache/file.h"
19 #include "net/disk_cache/net_log_parameters.h"
25 // Stream of the sparse data index.
26 const int kSparseIndex
= 2;
28 // Stream of the sparse data.
29 const int kSparseData
= 1;
31 // We can have up to 64k children.
32 const int kMaxMapSize
= 8 * 1024;
34 // The maximum number of bytes that a child can store.
35 const int kMaxEntrySize
= 0x100000;
37 // The size of each data block (tracked by the child allocation bitmap).
38 const int kBlockSize
= 1024;
40 // Returns the name of a child entry given the base_name and signature of the
41 // parent and the child_id.
42 // If the entry is called entry_name, child entries will be named something
43 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the
44 // number of the particular child.
45 std::string
GenerateChildName(const std::string
& base_name
, int64 signature
,
47 return base::StringPrintf("Range_%s:%" PRIx64
":%" PRIx64
, base_name
.c_str(),
51 // This class deletes the children of a sparse entry.
53 : public base::RefCounted
<ChildrenDeleter
>,
54 public disk_cache::FileIOCallback
{
56 ChildrenDeleter(disk_cache::BackendImpl
* backend
, const std::string
& name
)
57 : backend_(backend
->GetWeakPtr()), name_(name
), signature_(0) {}
59 virtual void OnFileIOComplete(int bytes_copied
) OVERRIDE
;
61 // Two ways of deleting the children: if we have the children map, use Start()
62 // directly, otherwise pass the data address to ReadData().
63 void Start(char* buffer
, int len
);
64 void ReadData(disk_cache::Addr address
, int len
);
67 friend class base::RefCounted
<ChildrenDeleter
>;
68 virtual ~ChildrenDeleter() {}
70 void DeleteChildren();
72 base::WeakPtr
<disk_cache::BackendImpl
> backend_
;
74 disk_cache::Bitmap children_map_
;
76 scoped_ptr
<char[]> buffer_
;
77 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter
);
80 // This is the callback of the file operation.
81 void ChildrenDeleter::OnFileIOComplete(int bytes_copied
) {
82 char* buffer
= buffer_
.release();
83 Start(buffer
, bytes_copied
);
86 void ChildrenDeleter::Start(char* buffer
, int len
) {
87 buffer_
.reset(buffer
);
88 if (len
< static_cast<int>(sizeof(disk_cache::SparseData
)))
91 // Just copy the information from |buffer|, delete |buffer| and start deleting
93 disk_cache::SparseData
* data
=
94 reinterpret_cast<disk_cache::SparseData
*>(buffer
);
95 signature_
= data
->header
.signature
;
97 int num_bits
= (len
- sizeof(disk_cache::SparseHeader
)) * 8;
98 children_map_
.Resize(num_bits
, false);
99 children_map_
.SetMap(data
->bitmap
, num_bits
/ 32);
105 void ChildrenDeleter::ReadData(disk_cache::Addr address
, int len
) {
106 DCHECK(address
.is_block_file());
110 disk_cache::File
* file(backend_
->File(address
));
114 size_t file_offset
= address
.start_block() * address
.BlockSize() +
115 disk_cache::kBlockHeaderSize
;
117 buffer_
.reset(new char[len
]);
119 if (!file
->Read(buffer_
.get(), len
, file_offset
, this, &completed
))
123 OnFileIOComplete(len
);
125 // And wait until OnFileIOComplete gets called.
128 void ChildrenDeleter::DeleteChildren() {
130 if (!children_map_
.FindNextSetBit(&child_id
) || !backend_
.get()) {
131 // We are done. Just delete this object.
134 std::string child_name
= GenerateChildName(name_
, signature_
, child_id
);
135 backend_
->SyncDoomEntry(child_name
);
136 children_map_
.Set(child_id
, false);
138 // Post a task to delete the next child.
139 base::MessageLoop::current()->PostTask(
140 FROM_HERE
, base::Bind(&ChildrenDeleter::DeleteChildren
, this));
143 // Returns the NetLog event type corresponding to a SparseOperation.
144 net::NetLog::EventType
GetSparseEventType(
145 disk_cache::SparseControl::SparseOperation operation
) {
147 case disk_cache::SparseControl::kReadOperation
:
148 return net::NetLog::TYPE_SPARSE_READ
;
149 case disk_cache::SparseControl::kWriteOperation
:
150 return net::NetLog::TYPE_SPARSE_WRITE
;
151 case disk_cache::SparseControl::kGetRangeOperation
:
152 return net::NetLog::TYPE_SPARSE_GET_RANGE
;
155 return net::NetLog::TYPE_CANCELLED
;
159 // Logs the end event for |operation| on a child entry. Range operations log
160 // no events for each child they search through.
161 void LogChildOperationEnd(const net::BoundNetLog
& net_log
,
162 disk_cache::SparseControl::SparseOperation operation
,
164 if (net_log
.IsLoggingAllEvents()) {
165 net::NetLog::EventType event_type
;
167 case disk_cache::SparseControl::kReadOperation
:
168 event_type
= net::NetLog::TYPE_SPARSE_READ_CHILD_DATA
;
170 case disk_cache::SparseControl::kWriteOperation
:
171 event_type
= net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA
;
173 case disk_cache::SparseControl::kGetRangeOperation
:
179 net_log
.EndEventWithNetErrorCode(event_type
, result
);
185 namespace disk_cache
{
187 SparseControl::SparseControl(EntryImpl
* entry
)
190 operation_(kNoOperation
),
196 child_map_(child_data_
.bitmap
, kNumSparseBits
, kNumSparseBits
/ 32),
202 memset(&sparse_header_
, 0, sizeof(sparse_header_
));
203 memset(&child_data_
, 0, sizeof(child_data_
));
206 SparseControl::~SparseControl() {
213 int SparseControl::Init() {
216 // We should not have sparse data for the exposed entry.
217 if (entry_
->GetDataSize(kSparseData
))
218 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
220 // Now see if there is something where we store our data.
222 int data_len
= entry_
->GetDataSize(kSparseIndex
);
224 rv
= CreateSparseEntry();
226 rv
= OpenSparseEntry(data_len
);
234 bool SparseControl::CouldBeSparse() const {
237 if (entry_
->GetDataSize(kSparseData
))
240 // We don't verify the data, just see if it could be there.
241 return (entry_
->GetDataSize(kSparseIndex
) != 0);
244 int SparseControl::StartIO(SparseOperation op
, int64 offset
, net::IOBuffer
* buf
,
245 int buf_len
, const CompletionCallback
& callback
) {
247 // We don't support simultaneous IO for sparse data.
248 if (operation_
!= kNoOperation
)
249 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
251 if (offset
< 0 || buf_len
< 0)
252 return net::ERR_INVALID_ARGUMENT
;
254 // We only support up to 64 GB.
255 if (offset
+ buf_len
>= 0x1000000000LL
|| offset
+ buf_len
< 0)
256 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
258 DCHECK(!user_buf_
.get());
259 DCHECK(user_callback_
.is_null());
261 if (!buf
&& (op
== kReadOperation
|| op
== kWriteOperation
))
264 // Copy the operation parameters.
267 user_buf_
= buf
? new net::DrainableIOBuffer(buf
, buf_len
) : NULL
;
269 user_callback_
= callback
;
276 if (entry_
->net_log().IsLoggingAllEvents()) {
277 entry_
->net_log().BeginEvent(
278 GetSparseEventType(operation_
),
279 CreateNetLogSparseOperationCallback(offset_
, buf_len_
));
284 // Everything was done synchronously.
285 operation_
= kNoOperation
;
287 user_callback_
.Reset();
291 return net::ERR_IO_PENDING
;
294 int SparseControl::GetAvailableRange(int64 offset
, int len
, int64
* start
) {
296 // We don't support simultaneous IO for sparse data.
297 if (operation_
!= kNoOperation
)
298 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
302 range_found_
= false;
303 int result
= StartIO(
304 kGetRangeOperation
, offset
, NULL
, len
, CompletionCallback());
310 // This is a failure. We want to return a valid start value in any case.
312 return result
< 0 ? result
: 0; // Don't mask error codes to the caller.
315 void SparseControl::CancelIO() {
316 if (operation_
== kNoOperation
)
321 int SparseControl::ReadyToUse(const CompletionCallback
& callback
) {
325 // We'll grab another reference to keep this object alive because we just have
326 // one extra reference due to the pending IO operation itself, but we'll
327 // release that one before invoking user_callback_.
328 entry_
->AddRef(); // Balanced in DoAbortCallbacks.
329 abort_callbacks_
.push_back(callback
);
330 return net::ERR_IO_PENDING
;
334 void SparseControl::DeleteChildren(EntryImpl
* entry
) {
335 DCHECK(entry
->GetEntryFlags() & PARENT_ENTRY
);
336 int data_len
= entry
->GetDataSize(kSparseIndex
);
337 if (data_len
< static_cast<int>(sizeof(SparseData
)) ||
338 entry
->GetDataSize(kSparseData
))
341 int map_len
= data_len
- sizeof(SparseHeader
);
342 if (map_len
> kMaxMapSize
|| map_len
% 4)
347 entry
->GetData(kSparseIndex
, &buffer
, &address
);
348 if (!buffer
&& !address
.is_initialized())
351 entry
->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN
);
353 DCHECK(entry
->backend_
.get());
354 ChildrenDeleter
* deleter
= new ChildrenDeleter(entry
->backend_
.get(),
356 // The object will self destruct when finished.
360 base::MessageLoop::current()->PostTask(
362 base::Bind(&ChildrenDeleter::Start
, deleter
, buffer
, data_len
));
364 base::MessageLoop::current()->PostTask(
366 base::Bind(&ChildrenDeleter::ReadData
, deleter
, address
, data_len
));
370 // We are going to start using this entry to store sparse data, so we have to
371 // initialize our control info.
372 int SparseControl::CreateSparseEntry() {
373 if (CHILD_ENTRY
& entry_
->GetEntryFlags())
374 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
376 memset(&sparse_header_
, 0, sizeof(sparse_header_
));
377 sparse_header_
.signature
= Time::Now().ToInternalValue();
378 sparse_header_
.magic
= kIndexMagic
;
379 sparse_header_
.parent_key_len
= entry_
->GetKey().size();
380 children_map_
.Resize(kNumSparseBits
, true);
382 // Save the header. The bitmap is saved in the destructor.
383 scoped_refptr
<net::IOBuffer
> buf(
384 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_
)));
386 int rv
= entry_
->WriteData(kSparseIndex
, 0, buf
.get(), sizeof(sparse_header_
),
387 CompletionCallback(), false);
388 if (rv
!= sizeof(sparse_header_
)) {
389 DLOG(ERROR
) << "Unable to save sparse_header_";
390 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
393 entry_
->SetEntryFlags(PARENT_ENTRY
);
397 // We are opening an entry from disk. Make sure that our control data is there.
398 int SparseControl::OpenSparseEntry(int data_len
) {
399 if (data_len
< static_cast<int>(sizeof(SparseData
)))
400 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
402 if (entry_
->GetDataSize(kSparseData
))
403 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
405 if (!(PARENT_ENTRY
& entry_
->GetEntryFlags()))
406 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
408 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB.
409 int map_len
= data_len
- sizeof(sparse_header_
);
410 if (map_len
> kMaxMapSize
|| map_len
% 4)
411 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
413 scoped_refptr
<net::IOBuffer
> buf(
414 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_
)));
417 int rv
= entry_
->ReadData(kSparseIndex
, 0, buf
.get(), sizeof(sparse_header_
),
418 CompletionCallback());
419 if (rv
!= static_cast<int>(sizeof(sparse_header_
)))
420 return net::ERR_CACHE_READ_FAILURE
;
422 // The real validation should be performed by the caller. This is just to
424 if (sparse_header_
.magic
!= kIndexMagic
||
425 sparse_header_
.parent_key_len
!=
426 static_cast<int>(entry_
->GetKey().size()))
427 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED
;
429 // Read the actual bitmap.
430 buf
= new net::IOBuffer(map_len
);
431 rv
= entry_
->ReadData(kSparseIndex
, sizeof(sparse_header_
), buf
.get(),
432 map_len
, CompletionCallback());
434 return net::ERR_CACHE_READ_FAILURE
;
436 // Grow the bitmap to the current size and copy the bits.
437 children_map_
.Resize(map_len
* 8, false);
438 children_map_
.SetMap(reinterpret_cast<uint32
*>(buf
->data()), map_len
);
442 bool SparseControl::OpenChild() {
443 DCHECK_GE(result_
, 0);
445 std::string key
= GenerateChildKey();
447 // Keep using the same child or open another one?.
448 if (key
== child_
->GetKey())
453 // See if we are tracking this child.
455 return ContinueWithoutChild(key
);
457 if (!entry_
->backend_
.get())
460 child_
= entry_
->backend_
->OpenEntryImpl(key
);
462 return ContinueWithoutChild(key
);
464 EntryImpl
* child
= static_cast<EntryImpl
*>(child_
);
465 if (!(CHILD_ENTRY
& child
->GetEntryFlags()) ||
466 child
->GetDataSize(kSparseIndex
) <
467 static_cast<int>(sizeof(child_data_
)))
468 return KillChildAndContinue(key
, false);
470 scoped_refptr
<net::WrappedIOBuffer
> buf(
471 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_
)));
474 int rv
= child_
->ReadData(kSparseIndex
, 0, buf
.get(), sizeof(child_data_
),
475 CompletionCallback());
476 if (rv
!= sizeof(child_data_
))
477 return KillChildAndContinue(key
, true); // This is a fatal failure.
479 if (child_data_
.header
.signature
!= sparse_header_
.signature
||
480 child_data_
.header
.magic
!= kIndexMagic
)
481 return KillChildAndContinue(key
, false);
483 if (child_data_
.header
.last_block_len
< 0 ||
484 child_data_
.header
.last_block_len
> kBlockSize
) {
485 // Make sure these values are always within range.
486 child_data_
.header
.last_block_len
= 0;
487 child_data_
.header
.last_block
= -1;
493 void SparseControl::CloseChild() {
494 scoped_refptr
<net::WrappedIOBuffer
> buf(
495 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_
)));
497 // Save the allocation bitmap before closing the child entry.
498 int rv
= child_
->WriteData(kSparseIndex
, 0, buf
.get(), sizeof(child_data_
),
499 CompletionCallback(), false);
500 if (rv
!= sizeof(child_data_
))
501 DLOG(ERROR
) << "Failed to save child data";
506 std::string
SparseControl::GenerateChildKey() {
507 return GenerateChildName(entry_
->GetKey(), sparse_header_
.signature
,
511 // We are deleting the child because something went wrong.
512 bool SparseControl::KillChildAndContinue(const std::string
& key
, bool fatal
) {
518 result_
= net::ERR_CACHE_READ_FAILURE
;
521 return ContinueWithoutChild(key
);
524 // We were not able to open this child; see what we can do.
525 bool SparseControl::ContinueWithoutChild(const std::string
& key
) {
526 if (kReadOperation
== operation_
)
528 if (kGetRangeOperation
== operation_
)
531 if (!entry_
->backend_
.get())
534 child_
= entry_
->backend_
->CreateEntryImpl(key
);
537 result_
= net::ERR_CACHE_READ_FAILURE
;
545 bool SparseControl::ChildPresent() {
546 int child_bit
= static_cast<int>(offset_
>> 20);
547 if (children_map_
.Size() <= child_bit
)
550 return children_map_
.Get(child_bit
);
553 void SparseControl::SetChildBit(bool value
) {
554 int child_bit
= static_cast<int>(offset_
>> 20);
556 // We may have to increase the bitmap of child entries.
557 if (children_map_
.Size() <= child_bit
)
558 children_map_
.Resize(Bitmap::RequiredArraySize(child_bit
+ 1) * 32, true);
560 children_map_
.Set(child_bit
, value
);
563 void SparseControl::WriteSparseData() {
564 scoped_refptr
<net::IOBuffer
> buf(new net::WrappedIOBuffer(
565 reinterpret_cast<const char*>(children_map_
.GetMap())));
567 int len
= children_map_
.ArraySize() * 4;
568 int rv
= entry_
->WriteData(kSparseIndex
, sizeof(sparse_header_
), buf
.get(),
569 len
, CompletionCallback(), false);
571 DLOG(ERROR
) << "Unable to save sparse map";
575 bool SparseControl::VerifyRange() {
576 DCHECK_GE(result_
, 0);
578 child_offset_
= static_cast<int>(offset_
) & (kMaxEntrySize
- 1);
579 child_len_
= std::min(buf_len_
, kMaxEntrySize
- child_offset_
);
581 // We can write to (or get info from) anywhere in this child.
582 if (operation_
!= kReadOperation
)
585 // Check that there are no holes in this range.
586 int last_bit
= (child_offset_
+ child_len_
+ 1023) >> 10;
587 int start
= child_offset_
>> 10;
588 if (child_map_
.FindNextBit(&start
, last_bit
, false)) {
589 // Something is not here.
590 DCHECK_GE(child_data_
.header
.last_block_len
, 0);
591 DCHECK_LT(child_data_
.header
.last_block_len
, kMaxEntrySize
);
592 int partial_block_len
= PartialBlockLength(start
);
593 if (start
== child_offset_
>> 10) {
594 // It looks like we don't have anything.
595 if (partial_block_len
<= (child_offset_
& (kBlockSize
- 1)))
599 // We have the first part.
600 child_len_
= (start
<< 10) - child_offset_
;
601 if (partial_block_len
) {
602 // We may have a few extra bytes.
603 child_len_
= std::min(child_len_
+ partial_block_len
, buf_len_
);
605 // There is no need to read more after this one.
606 buf_len_
= child_len_
;
611 void SparseControl::UpdateRange(int result
) {
612 if (result
<= 0 || operation_
!= kWriteOperation
)
615 DCHECK_GE(child_data_
.header
.last_block_len
, 0);
616 DCHECK_LT(child_data_
.header
.last_block_len
, kMaxEntrySize
);
619 int first_bit
= child_offset_
>> 10;
620 int block_offset
= child_offset_
& (kBlockSize
- 1);
621 if (block_offset
&& (child_data_
.header
.last_block
!= first_bit
||
622 child_data_
.header
.last_block_len
< block_offset
)) {
623 // The first block is not completely filled; ignore it.
627 int last_bit
= (child_offset_
+ result
) >> 10;
628 block_offset
= (child_offset_
+ result
) & (kBlockSize
- 1);
630 // This condition will hit with the following criteria:
631 // 1. The first byte doesn't follow the last write.
632 // 2. The first byte is in the middle of a block.
633 // 3. The first byte and the last byte are in the same block.
634 if (first_bit
> last_bit
)
637 if (block_offset
&& !child_map_
.Get(last_bit
)) {
638 // The last block is not completely filled; save it for later.
639 child_data_
.header
.last_block
= last_bit
;
640 child_data_
.header
.last_block_len
= block_offset
;
642 child_data_
.header
.last_block
= -1;
645 child_map_
.SetRange(first_bit
, last_bit
, true);
648 int SparseControl::PartialBlockLength(int block_index
) const {
649 if (block_index
== child_data_
.header
.last_block
)
650 return child_data_
.header
.last_block_len
;
652 // This may be the last stored index.
653 int entry_len
= child_
->GetDataSize(kSparseData
);
654 if (block_index
== entry_len
>> 10)
655 return entry_len
& (kBlockSize
- 1);
657 // This is really empty.
661 void SparseControl::InitChildData() {
662 // We know the real type of child_.
663 EntryImpl
* child
= static_cast<EntryImpl
*>(child_
);
664 child
->SetEntryFlags(CHILD_ENTRY
);
666 memset(&child_data_
, 0, sizeof(child_data_
));
667 child_data_
.header
= sparse_header_
;
669 scoped_refptr
<net::WrappedIOBuffer
> buf(
670 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_
)));
672 int rv
= child_
->WriteData(kSparseIndex
, 0, buf
.get(), sizeof(child_data_
),
673 CompletionCallback(), false);
674 if (rv
!= sizeof(child_data_
))
675 DLOG(ERROR
) << "Failed to save child data";
679 void SparseControl::DoChildrenIO() {
680 while (DoChildIO()) continue;
682 // Range operations are finished synchronously, often without setting
683 // |finished_| to true.
684 if (kGetRangeOperation
== operation_
&&
685 entry_
->net_log().IsLoggingAllEvents()) {
686 entry_
->net_log().EndEvent(
687 net::NetLog::TYPE_SPARSE_GET_RANGE
,
688 CreateNetLogGetAvailableRangeResultCallback(offset_
, result_
));
691 if (kGetRangeOperation
!= operation_
&&
692 entry_
->net_log().IsLoggingAllEvents()) {
693 entry_
->net_log().EndEvent(GetSparseEventType(operation_
));
696 DoUserCallback(); // Don't touch this object after this point.
700 bool SparseControl::DoChildIO() {
702 if (!buf_len_
|| result_
< 0)
711 // We have more work to do. Let's not trigger a callback to the caller.
713 CompletionCallback callback
;
714 if (!user_callback_
.is_null()) {
716 base::Bind(&SparseControl::OnChildIOCompleted
, base::Unretained(this));
720 switch (operation_
) {
722 if (entry_
->net_log().IsLoggingAllEvents()) {
723 entry_
->net_log().BeginEvent(
724 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA
,
725 CreateNetLogSparseReadWriteCallback(child_
->net_log().source(),
728 rv
= child_
->ReadDataImpl(kSparseData
, child_offset_
, user_buf_
.get(),
729 child_len_
, callback
);
731 case kWriteOperation
:
732 if (entry_
->net_log().IsLoggingAllEvents()) {
733 entry_
->net_log().BeginEvent(
734 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA
,
735 CreateNetLogSparseReadWriteCallback(child_
->net_log().source(),
738 rv
= child_
->WriteDataImpl(kSparseData
, child_offset_
, user_buf_
.get(),
739 child_len_
, callback
, false);
741 case kGetRangeOperation
:
742 rv
= DoGetAvailableRange();
748 if (rv
== net::ERR_IO_PENDING
) {
751 // The child will protect himself against closing the entry while IO is in
752 // progress. However, this entry can still be closed, and that would not
753 // be a good thing for us, so we increase the refcount until we're
754 // finished doing sparse stuff.
755 entry_
->AddRef(); // Balanced in DoUserCallback.
762 DoChildIOCompleted(rv
);
766 int SparseControl::DoGetAvailableRange() {
768 return child_len_
; // Move on to the next child.
770 // Check that there are no holes in this range.
771 int last_bit
= (child_offset_
+ child_len_
+ 1023) >> 10;
772 int start
= child_offset_
>> 10;
773 int partial_start_bytes
= PartialBlockLength(start
);
775 int bits_found
= child_map_
.FindBits(&found
, last_bit
, true);
777 // We don't care if there is a partial block in the middle of the range.
778 int block_offset
= child_offset_
& (kBlockSize
- 1);
779 if (!bits_found
&& partial_start_bytes
<= block_offset
)
782 // We are done. Just break the loop and reset result_ to our real result.
785 // found now points to the first 1. Lets see if we have zeros before it.
786 int empty_start
= std::max((found
<< 10) - child_offset_
, 0);
788 int bytes_found
= bits_found
<< 10;
789 bytes_found
+= PartialBlockLength(found
+ bits_found
);
792 bytes_found
-= block_offset
;
794 // If the user is searching past the end of this child, bits_found is the
795 // right result; otherwise, we have some empty space at the start of this
796 // query that we have to subtract from the range that we searched.
797 result_
= std::min(bytes_found
, child_len_
- empty_start
);
800 result_
= std::min(partial_start_bytes
- block_offset
, child_len_
);
804 // Only update offset_ when this query found zeros at the start.
806 offset_
+= empty_start
;
808 // This will actually break the loop.
813 void SparseControl::DoChildIOCompleted(int result
) {
814 LogChildOperationEnd(entry_
->net_log(), operation_
, result
);
816 // We fail the whole operation if we encounter an error.
827 // We'll be reusing the user provided buffer for the next chunk.
828 if (buf_len_
&& user_buf_
.get())
829 user_buf_
->DidConsume(result
);
832 void SparseControl::OnChildIOCompleted(int result
) {
833 DCHECK_NE(net::ERR_IO_PENDING
, result
);
834 DoChildIOCompleted(result
);
837 // We'll return the current result of the operation, which may be less than
838 // the bytes to read or write, but the user cancelled the operation.
840 if (entry_
->net_log().IsLoggingAllEvents()) {
841 entry_
->net_log().AddEvent(net::NetLog::TYPE_CANCELLED
);
842 entry_
->net_log().EndEvent(GetSparseEventType(operation_
));
844 // We have an indirect reference to this object for every callback so if
845 // there is only one callback, we may delete this object before reaching
847 bool has_abort_callbacks
= !abort_callbacks_
.empty();
849 if (has_abort_callbacks
)
854 // We are running a callback from the message loop. It's time to restart what
855 // we were doing before.
859 void SparseControl::DoUserCallback() {
860 DCHECK(!user_callback_
.is_null());
861 CompletionCallback cb
= user_callback_
;
862 user_callback_
.Reset();
865 operation_
= kNoOperation
;
867 entry_
->Release(); // Don't touch object after this line.
871 void SparseControl::DoAbortCallbacks() {
872 for (size_t i
= 0; i
< abort_callbacks_
.size(); i
++) {
873 // Releasing all references to entry_ may result in the destruction of this
874 // object so we should not be touching it after the last Release().
875 CompletionCallback cb
= abort_callbacks_
[i
];
876 if (i
== abort_callbacks_
.size() - 1)
877 abort_callbacks_
.clear();
879 entry_
->Release(); // Don't touch object after this line.
884 } // namespace disk_cache