entry_impl.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2006-2010 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. 4 5#include "net/disk_cache/entry_impl.h" 6 7#include "base/message_loop.h" 8#include "base/metrics/histogram.h" 9#include "base/string_util.h" 10#include "base/values.h" 11#include "net/base/io_buffer.h" 12#include "net/base/net_errors.h" 13#include "net/disk_cache/backend_impl.h" 14#include "net/disk_cache/bitmap.h" 15#include "net/disk_cache/cache_util.h" 16#include "net/disk_cache/histogram_macros.h" 17#include "net/disk_cache/sparse_control.h" 18 19using base::Time; 20using base::TimeDelta; 21using base::TimeTicks; 22 23namespace { 24 25// Index for the file used to store the key, if any (files_[kKeyFileIndex]). 26const int kKeyFileIndex = 3; 27 28// NetLog parameters for the creation of an EntryImpl. Contains an entry's name 29// and whether it was created or opened. 30class EntryCreationParameters : public net::NetLog::EventParameters { 31 public: 32 EntryCreationParameters(const std::string& key, bool created) 33 : key_(key), created_(created) { 34 } 35 36 Value* ToValue() const { 37 DictionaryValue* dict = new DictionaryValue(); 38 dict->SetString("key", key_); 39 dict->SetBoolean("created", created_); 40 return dict; 41 } 42 43 private: 44 const std::string key_; 45 const bool created_; 46 47 DISALLOW_COPY_AND_ASSIGN(EntryCreationParameters); 48}; 49 50// NetLog parameters for non-sparse reading and writing to an EntryImpl. 51class ReadWriteDataParams : public net::NetLog::EventParameters { 52 public: 53 // For reads, |truncate| must be false. 54 ReadWriteDataParams(int index, int offset, int buf_len, bool truncate) 55 : index_(index), offset_(offset), buf_len_(buf_len), truncate_(truncate) { 56 } 57 58 Value* ToValue() const { 59 DictionaryValue* dict = new DictionaryValue(); 60 dict->SetInteger("index", index_); 61 dict->SetInteger("offset", offset_); 62 dict->SetInteger("buf_len", buf_len_); 63 if (truncate_) 64 dict->SetBoolean("truncate", truncate_); 65 return dict; 66 } 67 68 private: 69 const int index_; 70 const int offset_; 71 const int buf_len_; 72 const bool truncate_; 73 74 DISALLOW_COPY_AND_ASSIGN(ReadWriteDataParams); 75}; 76 77// NetLog parameters logged when non-sparse reads and writes complete. 78class FileIOCompleteParameters : public net::NetLog::EventParameters { 79 public: 80 // |bytes_copied| is either the number of bytes copied or a network error 81 // code. |bytes_copied| must not be ERR_IO_PENDING, as it's not a valid 82 // result for an operation. 83 explicit FileIOCompleteParameters(int bytes_copied) 84 : bytes_copied_(bytes_copied) { 85 } 86 87 Value* ToValue() const { 88 DCHECK_NE(bytes_copied_, net::ERR_IO_PENDING); 89 DictionaryValue* dict = new DictionaryValue(); 90 if (bytes_copied_ < 0) { 91 dict->SetInteger("net_error", bytes_copied_); 92 } else { 93 dict->SetInteger("bytes_copied", bytes_copied_); 94 } 95 return dict; 96 } 97 98 private: 99 const int bytes_copied_; 100 101 DISALLOW_COPY_AND_ASSIGN(FileIOCompleteParameters); 102}; 103 104// This class implements FileIOCallback to buffer the callback from a file IO 105// operation from the actual net class. 106class SyncCallback: public disk_cache::FileIOCallback { 107 public: 108 // |end_event_type| is the event type to log on completion. Logs nothing on 109 // discard, or when the NetLog is not set to log all events. 110 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, 111 net::CompletionCallback* callback, 112 net::NetLog::EventType end_event_type) 113 : entry_(entry), callback_(callback), buf_(buffer), 114 start_(TimeTicks::Now()), end_event_type_(end_event_type) { 115 entry->AddRef(); 116 entry->IncrementIoCount(); 117 } 118 ~SyncCallback() {} 119 120 virtual void OnFileIOComplete(int bytes_copied); 121 void Discard(); 122 123 private: 124 disk_cache::EntryImpl* entry_; 125 net::CompletionCallback* callback_; 126 scoped_refptr<net::IOBuffer> buf_; 127 TimeTicks start_; 128 net::NetLog::EventType end_event_type_; 129 130 DISALLOW_COPY_AND_ASSIGN(SyncCallback); 131}; 132 133void SyncCallback::OnFileIOComplete(int bytes_copied) { 134 entry_->DecrementIoCount(); 135 if (callback_) { 136 if (entry_->net_log().IsLoggingAllEvents()) { 137 entry_->net_log().EndEvent( 138 end_event_type_, 139 make_scoped_refptr(new FileIOCompleteParameters(bytes_copied))); 140 } 141 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_); 142 callback_->Run(bytes_copied); 143 } 144 entry_->Release(); 145 delete this; 146} 147 148void SyncCallback::Discard() { 149 callback_ = NULL; 150 buf_ = NULL; 151 OnFileIOComplete(0); 152} 153 154const int kMaxBufferSize = 1024 * 1024; // 1 MB. 155 156} // namespace 157 158namespace disk_cache { 159 160// This class handles individual memory buffers that store data before it is 161// sent to disk. The buffer can start at any offset, but if we try to write to 162// anywhere in the first 16KB of the file (kMaxBlockSize), we set the offset to 163// zero. The buffer grows up to a size determined by the backend, to keep the 164// total memory used under control. 165class EntryImpl::UserBuffer { 166 public: 167 explicit UserBuffer(BackendImpl* backend) 168 : backend_(backend->GetWeakPtr()), offset_(0), grow_allowed_(true) { 169 buffer_.reserve(kMaxBlockSize); 170 } 171 ~UserBuffer() { 172 if (backend_) 173 backend_->BufferDeleted(capacity() - kMaxBlockSize); 174 } 175 176 // Returns true if we can handle writing |len| bytes to |offset|. 177 bool PreWrite(int offset, int len); 178 179 // Truncates the buffer to |offset| bytes. 180 void Truncate(int offset); 181 182 // Writes |len| bytes from |buf| at the given |offset|. 183 void Write(int offset, net::IOBuffer* buf, int len); 184 185 // Returns true if we can read |len| bytes from |offset|, given that the 186 // actual file has |eof| bytes stored. Note that the number of bytes to read 187 // may be modified by this method even though it returns false: that means we 188 // should do a smaller read from disk. 189 bool PreRead(int eof, int offset, int* len); 190 191 // Read |len| bytes from |buf| at the given |offset|. 192 int Read(int offset, net::IOBuffer* buf, int len); 193 194 // Prepare this buffer for reuse. 195 void Reset(); 196 197 char* Data() { return buffer_.size() ? &buffer_[0] : NULL; } 198 int Size() { return static_cast<int>(buffer_.size()); } 199 int Start() { return offset_; } 200 int End() { return offset_ + Size(); } 201 202 private: 203 int capacity() { return static_cast<int>(buffer_.capacity()); } 204 bool GrowBuffer(int required, int limit); 205 206 base::WeakPtr<BackendImpl> backend_; 207 int offset_; 208 std::vector<char> buffer_; 209 bool grow_allowed_; 210 DISALLOW_COPY_AND_ASSIGN(UserBuffer); 211}; 212 213bool EntryImpl::UserBuffer::PreWrite(int offset, int len) { 214 DCHECK_GE(offset, 0); 215 DCHECK_GE(len, 0); 216 DCHECK_GE(offset + len, 0); 217 218 // We don't want to write before our current start. 219 if (offset < offset_) 220 return false; 221 222 // Lets get the common case out of the way. 223 if (offset + len <= capacity()) 224 return true; 225 226 // If we are writing to the first 16K (kMaxBlockSize), we want to keep the 227 // buffer offset_ at 0. 228 if (!Size() && offset > kMaxBlockSize) 229 return GrowBuffer(len, kMaxBufferSize); 230 231 int required = offset - offset_ + len; 232 return GrowBuffer(required, kMaxBufferSize * 6 / 5); 233} 234 235void EntryImpl::UserBuffer::Truncate(int offset) { 236 DCHECK_GE(offset, 0); 237 DCHECK_GE(offset, offset_); 238 DVLOG(3) << "Buffer truncate at " << offset << " current " << offset_; 239 240 offset -= offset_; 241 if (Size() >= offset) 242 buffer_.resize(offset); 243} 244 245void EntryImpl::UserBuffer::Write(int offset, net::IOBuffer* buf, int len) { 246 DCHECK_GE(offset, 0); 247 DCHECK_GE(len, 0); 248 DCHECK_GE(offset + len, 0); 249 DCHECK_GE(offset, offset_); 250 DVLOG(3) << "Buffer write at " << offset << " current " << offset_; 251 252 if (!Size() && offset > kMaxBlockSize) 253 offset_ = offset; 254 255 offset -= offset_; 256 257 if (offset > Size()) 258 buffer_.resize(offset); 259 260 if (!len) 261 return; 262 263 char* buffer = buf->data(); 264 int valid_len = Size() - offset; 265 int copy_len = std::min(valid_len, len); 266 if (copy_len) { 267 memcpy(&buffer_[offset], buffer, copy_len); 268 len -= copy_len; 269 buffer += copy_len; 270 } 271 if (!len) 272 return; 273 274 buffer_.insert(buffer_.end(), buffer, buffer + len); 275} 276 277bool EntryImpl::UserBuffer::PreRead(int eof, int offset, int* len) { 278 DCHECK_GE(offset, 0); 279 DCHECK_GT(*len, 0); 280 281 if (offset < offset_) { 282 // We are reading before this buffer. 283 if (offset >= eof) 284 return true; 285 286 // If the read overlaps with the buffer, change its length so that there is 287 // no overlap. 288 *len = std::min(*len, offset_ - offset); 289 *len = std::min(*len, eof - offset); 290 291 // We should read from disk. 292 return false; 293 } 294 295 if (!Size()) 296 return false; 297 298 // See if we can fulfill the first part of the operation. 299 return (offset - offset_ < Size()); 300} 301 302int EntryImpl::UserBuffer::Read(int offset, net::IOBuffer* buf, int len) { 303 DCHECK_GE(offset, 0); 304 DCHECK_GT(len, 0); 305 DCHECK(Size() || offset < offset_); 306 307 int clean_bytes = 0; 308 if (offset < offset_) { 309 // We don't have a file so lets fill the first part with 0. 310 clean_bytes = std::min(offset_ - offset, len); 311 memset(buf->data(), 0, clean_bytes); 312 if (len == clean_bytes) 313 return len; 314 offset = offset_; 315 len -= clean_bytes; 316 } 317 318 int start = offset - offset_; 319 int available = Size() - start; 320 DCHECK_GE(start, 0); 321 DCHECK_GE(available, 0); 322 len = std::min(len, available); 323 memcpy(buf->data() + clean_bytes, &buffer_[start], len); 324 return len + clean_bytes; 325} 326 327void EntryImpl::UserBuffer::Reset() { 328 if (!grow_allowed_) { 329 if (backend_) 330 backend_->BufferDeleted(capacity() - kMaxBlockSize); 331 grow_allowed_ = true; 332 std::vector<char> tmp; 333 buffer_.swap(tmp); 334 buffer_.reserve(kMaxBlockSize); 335 } 336 offset_ = 0; 337 buffer_.clear(); 338} 339 340bool EntryImpl::UserBuffer::GrowBuffer(int required, int limit) { 341 DCHECK_GE(required, 0); 342 int current_size = capacity(); 343 if (required <= current_size) 344 return true; 345 346 if (required > limit) 347 return false; 348 349 if (!backend_) 350 return false; 351 352 int to_add = std::max(required - current_size, kMaxBlockSize * 4); 353 to_add = std::max(current_size, to_add); 354 required = std::min(current_size + to_add, limit); 355 356 grow_allowed_ = backend_->IsAllocAllowed(current_size, required); 357 if (!grow_allowed_) 358 return false; 359 360 DVLOG(3) << "Buffer grow to " << required; 361 362 buffer_.reserve(required); 363 return true; 364} 365 366// ------------------------------------------------------------------------ 367 368EntryImpl::EntryImpl(BackendImpl* backend, Addr address, bool read_only) 369 : entry_(NULL, Addr(0)), node_(NULL, Addr(0)), read_only_(read_only) { 370 entry_.LazyInit(backend->File(address), address); 371 doomed_ = false; 372 backend_ = backend; 373 for (int i = 0; i < kNumStreams; i++) { 374 unreported_size_[i] = 0; 375 } 376} 377 378void EntryImpl::DoomImpl() { 379 if (doomed_) 380 return; 381 382 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); 383 backend_->InternalDoomEntry(this); 384} 385 386int EntryImpl::ReadDataImpl(int index, int offset, net::IOBuffer* buf, 387 int buf_len, CompletionCallback* callback) { 388 if (net_log_.IsLoggingAllEvents()) { 389 net_log_.BeginEvent( 390 net::NetLog::TYPE_DISK_CACHE_READ_DATA, 391 make_scoped_refptr( 392 new ReadWriteDataParams(index, offset, buf_len, false))); 393 } 394 395 int result = InternalReadData(index, offset, buf, buf_len, callback); 396 397 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { 398 net_log_.EndEvent( 399 net::NetLog::TYPE_DISK_CACHE_READ_DATA, 400 make_scoped_refptr(new FileIOCompleteParameters(result))); 401 } 402 return result; 403} 404 405int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf, 406 int buf_len, CompletionCallback* callback, 407 bool truncate) { 408 if (net_log_.IsLoggingAllEvents()) { 409 net_log_.BeginEvent( 410 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA, 411 make_scoped_refptr( 412 new ReadWriteDataParams(index, offset, buf_len, truncate))); 413 } 414 415 int result = InternalWriteData(index, offset, buf, buf_len, callback, 416 truncate); 417 418 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) { 419 net_log_.EndEvent( 420 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA, 421 make_scoped_refptr(new FileIOCompleteParameters(result))); 422 } 423 return result; 424} 425 426int EntryImpl::ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, 427 CompletionCallback* callback) { 428 DCHECK(node_.Data()->dirty || read_only_); 429 int result = InitSparseData(); 430 if (net::OK != result) 431 return result; 432 433 TimeTicks start = TimeTicks::Now(); 434 result = sparse_->StartIO(SparseControl::kReadOperation, offset, buf, buf_len, 435 callback); 436 ReportIOTime(kSparseRead, start); 437 return result; 438} 439 440int EntryImpl::WriteSparseDataImpl(int64 offset, net::IOBuffer* buf, 441 int buf_len, CompletionCallback* callback) { 442 DCHECK(node_.Data()->dirty || read_only_); 443 int result = InitSparseData(); 444 if (net::OK != result) 445 return result; 446 447 TimeTicks start = TimeTicks::Now(); 448 result = sparse_->StartIO(SparseControl::kWriteOperation, offset, buf, 449 buf_len, callback); 450 ReportIOTime(kSparseWrite, start); 451 return result; 452} 453 454int EntryImpl::GetAvailableRangeImpl(int64 offset, int len, int64* start) { 455 int result = InitSparseData(); 456 if (net::OK != result) 457 return result; 458 459 return sparse_->GetAvailableRange(offset, len, start); 460} 461 462void EntryImpl::CancelSparseIOImpl() { 463 if (!sparse_.get()) 464 return; 465 466 sparse_->CancelIO(); 467} 468 469int EntryImpl::ReadyForSparseIOImpl(CompletionCallback* callback) { 470 DCHECK(sparse_.get()); 471 return sparse_->ReadyToUse(callback); 472} 473 474uint32 EntryImpl::GetHash() { 475 return entry_.Data()->hash; 476} 477 478bool EntryImpl::CreateEntry(Addr node_address, const std::string& key, 479 uint32 hash) { 480 Trace("Create entry In"); 481 EntryStore* entry_store = entry_.Data(); 482 RankingsNode* node = node_.Data(); 483 memset(entry_store, 0, sizeof(EntryStore) * entry_.address().num_blocks()); 484 memset(node, 0, sizeof(RankingsNode)); 485 if (!node_.LazyInit(backend_->File(node_address), node_address)) 486 return false; 487 488 entry_store->rankings_node = node_address.value(); 489 node->contents = entry_.address().value(); 490 491 entry_store->hash = hash; 492 entry_store->creation_time = Time::Now().ToInternalValue(); 493 entry_store->key_len = static_cast<int32>(key.size()); 494 if (entry_store->key_len > kMaxInternalKeyLength) { 495 Addr address(0); 496 if (!CreateBlock(entry_store->key_len + 1, &address)) 497 return false; 498 499 entry_store->long_key = address.value(); 500 File* key_file = GetBackingFile(address, kKeyFileIndex); 501 key_ = key; 502 503 size_t offset = 0; 504 if (address.is_block_file()) 505 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; 506 507 if (!key_file || !key_file->Write(key.data(), key.size(), offset)) { 508 DeleteData(address, kKeyFileIndex); 509 return false; 510 } 511 512 if (address.is_separate_file()) 513 key_file->SetLength(key.size() + 1); 514 } else { 515 memcpy(entry_store->key, key.data(), key.size()); 516 entry_store->key[key.size()] = '\0'; 517 } 518 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); 519 CACHE_UMA(COUNTS, "KeySize", 0, static_cast<int32>(key.size())); 520 node->dirty = backend_->GetCurrentEntryId(); 521 Log("Create Entry "); 522 return true; 523} 524 525bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) { 526 if (entry_.Data()->hash != hash || 527 static_cast<size_t>(entry_.Data()->key_len) != key.size()) 528 return false; 529 530 std::string my_key = GetKey(); 531 return key.compare(my_key) ? false : true; 532} 533 534void EntryImpl::InternalDoom() { 535 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_DOOM, NULL); 536 DCHECK(node_.HasData()); 537 if (!node_.Data()->dirty) { 538 node_.Data()->dirty = backend_->GetCurrentEntryId(); 539 node_.Store(); 540 } 541 doomed_ = true; 542} 543 544void EntryImpl::DeleteEntryData(bool everything) { 545 DCHECK(doomed_ || !everything); 546 547 if (GetEntryFlags() & PARENT_ENTRY) { 548 // We have some child entries that must go away. 549 SparseControl::DeleteChildren(this); 550 } 551 552 if (GetDataSize(0)) 553 CACHE_UMA(COUNTS, "DeleteHeader", 0, GetDataSize(0)); 554 if (GetDataSize(1)) 555 CACHE_UMA(COUNTS, "DeleteData", 0, GetDataSize(1)); 556 for (int index = 0; index < kNumStreams; index++) { 557 Addr address(entry_.Data()->data_addr[index]); 558 if (address.is_initialized()) { 559 backend_->ModifyStorageSize(entry_.Data()->data_size[index] - 560 unreported_size_[index], 0); 561 entry_.Data()->data_addr[index] = 0; 562 entry_.Data()->data_size[index] = 0; 563 entry_.Store(); 564 DeleteData(address, index); 565 } 566 } 567 568 if (!everything) 569 return; 570 571 // Remove all traces of this entry. 572 backend_->RemoveEntry(this); 573 574 Addr address(entry_.Data()->long_key); 575 DeleteData(address, kKeyFileIndex); 576 backend_->ModifyStorageSize(entry_.Data()->key_len, 0); 577 578 memset(node_.buffer(), 0, node_.size()); 579 memset(entry_.buffer(), 0, entry_.size()); 580 node_.Store(); 581 entry_.Store(); 582 583 backend_->DeleteBlock(node_.address(), false); 584 backend_->DeleteBlock(entry_.address(), false); 585} 586 587CacheAddr EntryImpl::GetNextAddress() { 588 return entry_.Data()->next; 589} 590 591void EntryImpl::SetNextAddress(Addr address) { 592 entry_.Data()->next = address.value(); 593 bool success = entry_.Store(); 594 DCHECK(success); 595} 596 597bool EntryImpl::LoadNodeAddress() { 598 Addr address(entry_.Data()->rankings_node); 599 if (!node_.LazyInit(backend_->File(address), address)) 600 return false; 601 return node_.Load(); 602} 603 604bool EntryImpl::Update() { 605 DCHECK(node_.HasData()); 606 607 if (read_only_) 608 return true; 609 610 RankingsNode* rankings = node_.Data(); 611 if (!rankings->dirty) { 612 rankings->dirty = backend_->GetCurrentEntryId(); 613 if (!node_.Store()) 614 return false; 615 } 616 return true; 617} 618 619bool EntryImpl::IsDirty(int32 current_id) { 620 DCHECK(node_.HasData()); 621 // We are checking if the entry is valid or not. If there is a pointer here, 622 // we should not be checking the entry. 623 if (node_.Data()->dummy) 624 return true; 625 626 return node_.Data()->dirty && current_id != node_.Data()->dirty; 627} 628 629void EntryImpl::ClearDirtyFlag() { 630 node_.Data()->dirty = 0; 631} 632 633void EntryImpl::SetPointerForInvalidEntry(int32 new_id) { 634 node_.Data()->dirty = new_id; 635 node_.Data()->dummy = 0; 636 node_.Store(); 637} 638 639bool EntryImpl::SanityCheck() { 640 if (!entry_.Data()->rankings_node || !entry_.Data()->key_len) 641 return false; 642 643 Addr rankings_addr(entry_.Data()->rankings_node); 644 if (!rankings_addr.is_initialized() || rankings_addr.is_separate_file() || 645 rankings_addr.file_type() != RANKINGS) 646 return false; 647 648 Addr next_addr(entry_.Data()->next); 649 if (next_addr.is_initialized() && 650 (next_addr.is_separate_file() || next_addr.file_type() != BLOCK_256)) 651 return false; 652 653 return true; 654} 655 656void EntryImpl::IncrementIoCount() { 657 backend_->IncrementIoCount(); 658} 659 660void EntryImpl::DecrementIoCount() { 661 backend_->DecrementIoCount(); 662} 663 664void EntryImpl::SetTimes(base::Time last_used, base::Time last_modified) { 665 node_.Data()->last_used = last_used.ToInternalValue(); 666 node_.Data()->last_modified = last_modified.ToInternalValue(); 667 node_.set_modified(); 668} 669 670void EntryImpl::ReportIOTime(Operation op, const base::TimeTicks& start) { 671 int group = backend_->GetSizeGroup(); 672 switch (op) { 673 case kRead: 674 CACHE_UMA(AGE_MS, "ReadTime", group, start); 675 break; 676 case kWrite: 677 CACHE_UMA(AGE_MS, "WriteTime", group, start); 678 break; 679 case kSparseRead: 680 CACHE_UMA(AGE_MS, "SparseReadTime", 0, start); 681 break; 682 case kSparseWrite: 683 CACHE_UMA(AGE_MS, "SparseWriteTime", 0, start); 684 break; 685 case kAsyncIO: 686 CACHE_UMA(AGE_MS, "AsyncIOTime", group, start); 687 break; 688 default: 689 NOTREACHED(); 690 } 691} 692 693void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) { 694 DCHECK(!net_log_.net_log()); 695 net_log_ = net::BoundNetLog::Make( 696 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY); 697 net_log_.BeginEvent( 698 net::NetLog::TYPE_DISK_CACHE_ENTRY, 699 make_scoped_refptr(new EntryCreationParameters(GetKey(), created))); 700} 701 702const net::BoundNetLog& EntryImpl::net_log() const { 703 return net_log_; 704} 705 706void EntryImpl::Doom() { 707 backend_->background_queue()->DoomEntryImpl(this); 708} 709 710void EntryImpl::Close() { 711 backend_->background_queue()->CloseEntryImpl(this); 712} 713 714std::string EntryImpl::GetKey() const { 715 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); 716 if (entry->Data()->key_len <= kMaxInternalKeyLength) 717 return std::string(entry->Data()->key); 718 719 // We keep a copy of the key so that we can always return it, even if the 720 // backend is disabled. 721 if (!key_.empty()) 722 return key_; 723 724 Addr address(entry->Data()->long_key); 725 DCHECK(address.is_initialized()); 726 size_t offset = 0; 727 if (address.is_block_file()) 728 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; 729 730 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index); 731 File* key_file = const_cast<EntryImpl*>(this)->GetBackingFile(address, 732 kKeyFileIndex); 733 734 if (!key_file || 735 !key_file->Read(WriteInto(&key_, entry->Data()->key_len + 1), 736 entry->Data()->key_len + 1, offset)) 737 key_.clear(); 738 return key_; 739} 740 741Time EntryImpl::GetLastUsed() const { 742 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); 743 return Time::FromInternalValue(node->Data()->last_used); 744} 745 746Time EntryImpl::GetLastModified() const { 747 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_); 748 return Time::FromInternalValue(node->Data()->last_modified); 749} 750 751int32 EntryImpl::GetDataSize(int index) const { 752 if (index < 0 || index >= kNumStreams) 753 return 0; 754 755 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_); 756 return entry->Data()->data_size[index]; 757} 758 759int EntryImpl::ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, 760 net::CompletionCallback* callback) { 761 if (!callback) 762 return ReadDataImpl(index, offset, buf, buf_len, callback); 763 764 DCHECK(node_.Data()->dirty || read_only_); 765 if (index < 0 || index >= kNumStreams) 766 return net::ERR_INVALID_ARGUMENT; 767 768 int entry_size = entry_.Data()->data_size[index]; 769 if (offset >= entry_size || offset < 0 || !buf_len) 770 return 0; 771 772 if (buf_len < 0) 773 return net::ERR_INVALID_ARGUMENT; 774 775 backend_->background_queue()->ReadData(this, index, offset, buf, buf_len, 776 callback); 777 return net::ERR_IO_PENDING; 778} 779 780int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, 781 CompletionCallback* callback, bool truncate) { 782 if (!callback) 783 return WriteDataImpl(index, offset, buf, buf_len, callback, truncate); 784 785 DCHECK(node_.Data()->dirty || read_only_); 786 if (index < 0 || index >= kNumStreams) 787 return net::ERR_INVALID_ARGUMENT; 788 789 if (offset < 0 || buf_len < 0) 790 return net::ERR_INVALID_ARGUMENT; 791 792 backend_->background_queue()->WriteData(this, index, offset, buf, buf_len, 793 truncate, callback); 794 return net::ERR_IO_PENDING; 795} 796 797int EntryImpl::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 798 net::CompletionCallback* callback) { 799 if (!callback) 800 return ReadSparseDataImpl(offset, buf, buf_len, callback); 801 802 backend_->background_queue()->ReadSparseData(this, offset, buf, buf_len, 803 callback); 804 return net::ERR_IO_PENDING; 805} 806 807int EntryImpl::WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 808 net::CompletionCallback* callback) { 809 if (!callback) 810 return WriteSparseDataImpl(offset, buf, buf_len, callback); 811 812 backend_->background_queue()->WriteSparseData(this, offset, buf, buf_len, 813 callback); 814 return net::ERR_IO_PENDING; 815} 816 817int EntryImpl::GetAvailableRange(int64 offset, int len, int64* start, 818 CompletionCallback* callback) { 819 backend_->background_queue()->GetAvailableRange(this, offset, len, start, 820 callback); 821 return net::ERR_IO_PENDING; 822} 823 824bool EntryImpl::CouldBeSparse() const { 825 if (sparse_.get()) 826 return true; 827 828 scoped_ptr<SparseControl> sparse; 829 sparse.reset(new SparseControl(const_cast<EntryImpl*>(this))); 830 return sparse->CouldBeSparse(); 831} 832 833void EntryImpl::CancelSparseIO() { 834 backend_->background_queue()->CancelSparseIO(this); 835} 836 837int EntryImpl::ReadyForSparseIO(net::CompletionCallback* callback) { 838 if (!sparse_.get()) 839 return net::OK; 840 841 backend_->background_queue()->ReadyForSparseIO(this, callback); 842 return net::ERR_IO_PENDING; 843} 844 845// When an entry is deleted from the cache, we clean up all the data associated 846// with it for two reasons: to simplify the reuse of the block (we know that any 847// unused block is filled with zeros), and to simplify the handling of write / 848// read partial information from an entry (don't have to worry about returning 849// data related to a previous cache entry because the range was not fully 850// written before). 851EntryImpl::~EntryImpl() { 852 Log("~EntryImpl in"); 853 854 // Save the sparse info to disk. This will generate IO for this entry and 855 // maybe for a child entry, so it is important to do it before deleting this 856 // entry. 857 sparse_.reset(); 858 859 // Remove this entry from the list of open entries. 860 backend_->OnEntryDestroyBegin(entry_.address()); 861 862 if (doomed_) { 863 DeleteEntryData(true); 864 } else { 865 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_CLOSE, NULL); 866 bool ret = true; 867 for (int index = 0; index < kNumStreams; index++) { 868 if (user_buffers_[index].get()) { 869 if (!(ret = Flush(index, 0))) 870 LOG(ERROR) << "Failed to save user data"; 871 } 872 if (unreported_size_[index]) { 873 backend_->ModifyStorageSize( 874 entry_.Data()->data_size[index] - unreported_size_[index], 875 entry_.Data()->data_size[index]); 876 } 877 } 878 879 if (!ret) { 880 // There was a failure writing the actual data. Mark the entry as dirty. 881 int current_id = backend_->GetCurrentEntryId(); 882 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1; 883 node_.Store(); 884 } else if (node_.HasData() && node_.Data()->dirty) { 885 node_.Data()->dirty = 0; 886 node_.Store(); 887 } 888 } 889 890 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this)); 891 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY, NULL); 892 backend_->OnEntryDestroyEnd(); 893} 894 895// ------------------------------------------------------------------------ 896 897int EntryImpl::InternalReadData(int index, int offset, net::IOBuffer* buf, 898 int buf_len, CompletionCallback* callback) { 899 DCHECK(node_.Data()->dirty || read_only_); 900 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; 901 if (index < 0 || index >= kNumStreams) 902 return net::ERR_INVALID_ARGUMENT; 903 904 int entry_size = entry_.Data()->data_size[index]; 905 if (offset >= entry_size || offset < 0 || !buf_len) 906 return 0; 907 908 if (buf_len < 0) 909 return net::ERR_INVALID_ARGUMENT; 910 911 TimeTicks start = TimeTicks::Now(); 912 913 if (offset + buf_len > entry_size) 914 buf_len = entry_size - offset; 915 916 UpdateRank(false); 917 918 backend_->OnEvent(Stats::READ_DATA); 919 backend_->OnRead(buf_len); 920 921 Addr address(entry_.Data()->data_addr[index]); 922 int eof = address.is_initialized() ? entry_size : 0; 923 if (user_buffers_[index].get() && 924 user_buffers_[index]->PreRead(eof, offset, &buf_len)) { 925 // Complete the operation locally. 926 buf_len = user_buffers_[index]->Read(offset, buf, buf_len); 927 ReportIOTime(kRead, start); 928 return buf_len; 929 } 930 931 address.set_value(entry_.Data()->data_addr[index]); 932 DCHECK(address.is_initialized()); 933 if (!address.is_initialized()) 934 return net::ERR_FAILED; 935 936 File* file = GetBackingFile(address, index); 937 if (!file) 938 return net::ERR_FAILED; 939 940 size_t file_offset = offset; 941 if (address.is_block_file()) { 942 DCHECK_LE(offset + buf_len, kMaxBlockSize); 943 file_offset += address.start_block() * address.BlockSize() + 944 kBlockHeaderSize; 945 } 946 947 SyncCallback* io_callback = NULL; 948 if (callback) { 949 io_callback = new SyncCallback(this, buf, callback, 950 net::NetLog::TYPE_DISK_CACHE_READ_DATA); 951 } 952 953 bool completed; 954 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { 955 if (io_callback) 956 io_callback->Discard(); 957 return net::ERR_FAILED; 958 } 959 960 if (io_callback && completed) 961 io_callback->Discard(); 962 963 ReportIOTime(kRead, start); 964 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; 965} 966 967int EntryImpl::InternalWriteData(int index, int offset, net::IOBuffer* buf, 968 int buf_len, CompletionCallback* callback, 969 bool truncate) { 970 DCHECK(node_.Data()->dirty || read_only_); 971 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; 972 if (index < 0 || index >= kNumStreams) 973 return net::ERR_INVALID_ARGUMENT; 974 975 if (offset < 0 || buf_len < 0) 976 return net::ERR_INVALID_ARGUMENT; 977 978 int max_file_size = backend_->MaxFileSize(); 979 980 // offset or buf_len could be negative numbers. 981 if (offset > max_file_size || buf_len > max_file_size || 982 offset + buf_len > max_file_size) { 983 int size = offset + buf_len; 984 if (size <= max_file_size) 985 size = kint32max; 986 backend_->TooMuchStorageRequested(size); 987 return net::ERR_FAILED; 988 } 989 990 TimeTicks start = TimeTicks::Now(); 991 992 // Read the size at this point (it may change inside prepare). 993 int entry_size = entry_.Data()->data_size[index]; 994 bool extending = entry_size < offset + buf_len; 995 truncate = truncate && entry_size > offset + buf_len; 996 Trace("To PrepareTarget 0x%x", entry_.address().value()); 997 if (!PrepareTarget(index, offset, buf_len, truncate)) 998 return net::ERR_FAILED; 999 1000 Trace("From PrepareTarget 0x%x", entry_.address().value()); 1001 if (extending || truncate) 1002 UpdateSize(index, entry_size, offset + buf_len); 1003 1004 UpdateRank(true); 1005 1006 backend_->OnEvent(Stats::WRITE_DATA); 1007 backend_->OnWrite(buf_len); 1008 1009 if (user_buffers_[index].get()) { 1010 // Complete the operation locally. 1011 user_buffers_[index]->Write(offset, buf, buf_len); 1012 ReportIOTime(kWrite, start); 1013 return buf_len; 1014 } 1015 1016 Addr address(entry_.Data()->data_addr[index]); 1017 if (offset + buf_len == 0) { 1018 if (truncate) { 1019 DCHECK(!address.is_initialized()); 1020 } 1021 return 0; 1022 } 1023 1024 File* file = GetBackingFile(address, index); 1025 if (!file) 1026 return net::ERR_FAILED; 1027 1028 size_t file_offset = offset; 1029 if (address.is_block_file()) { 1030 DCHECK_LE(offset + buf_len, kMaxBlockSize); 1031 file_offset += address.start_block() * address.BlockSize() + 1032 kBlockHeaderSize; 1033 } else if (truncate || (extending && !buf_len)) { 1034 if (!file->SetLength(offset + buf_len)) 1035 return net::ERR_FAILED; 1036 } 1037 1038 if (!buf_len) 1039 return 0; 1040 1041 SyncCallback* io_callback = NULL; 1042 if (callback) { 1043 io_callback = new SyncCallback(this, buf, callback, 1044 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA); 1045 } 1046 1047 bool completed; 1048 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, 1049 &completed)) { 1050 if (io_callback) 1051 io_callback->Discard(); 1052 return net::ERR_FAILED; 1053 } 1054 1055 if (io_callback && completed) 1056 io_callback->Discard(); 1057 1058 ReportIOTime(kWrite, start); 1059 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; 1060} 1061 1062// ------------------------------------------------------------------------ 1063 1064bool EntryImpl::CreateDataBlock(int index, int size) { 1065 DCHECK(index >= 0 && index < kNumStreams); 1066 1067 Addr address(entry_.Data()->data_addr[index]); 1068 if (!CreateBlock(size, &address)) 1069 return false; 1070 1071 entry_.Data()->data_addr[index] = address.value(); 1072 entry_.Store(); 1073 return true; 1074} 1075 1076bool EntryImpl::CreateBlock(int size, Addr* address) { 1077 DCHECK(!address->is_initialized()); 1078 1079 FileType file_type = Addr::RequiredFileType(size); 1080 if (EXTERNAL == file_type) { 1081 if (size > backend_->MaxFileSize()) 1082 return false; 1083 if (!backend_->CreateExternalFile(address)) 1084 return false; 1085 } else { 1086 int num_blocks = (size + Addr::BlockSizeForFileType(file_type) - 1) / 1087 Addr::BlockSizeForFileType(file_type); 1088 1089 if (!backend_->CreateBlock(file_type, num_blocks, address)) 1090 return false; 1091 } 1092 return true; 1093} 1094 1095// Note that this method may end up modifying a block file so upon return the 1096// involved block will be free, and could be reused for something else. If there 1097// is a crash after that point (and maybe before returning to the caller), the 1098// entry will be left dirty... and at some point it will be discarded; it is 1099// important that the entry doesn't keep a reference to this address, or we'll 1100// end up deleting the contents of |address| once again. 1101void EntryImpl::DeleteData(Addr address, int index) { 1102 if (!address.is_initialized()) 1103 return; 1104 if (address.is_separate_file()) { 1105 int failure = !DeleteCacheFile(backend_->GetFileName(address)); 1106 CACHE_UMA(COUNTS, "DeleteFailed", 0, failure); 1107 if (failure) { 1108 LOG(ERROR) << "Failed to delete " << 1109 backend_->GetFileName(address).value() << " from the cache."; 1110 } 1111 if (files_[index]) 1112 files_[index] = NULL; // Releases the object. 1113 } else { 1114 backend_->DeleteBlock(address, true); 1115 } 1116} 1117 1118void EntryImpl::UpdateRank(bool modified) { 1119 if (!doomed_) { 1120 // Everything is handled by the backend. 1121 backend_->UpdateRank(this, modified); 1122 return; 1123 } 1124 1125 Time current = Time::Now(); 1126 node_.Data()->last_used = current.ToInternalValue(); 1127 1128 if (modified) 1129 node_.Data()->last_modified = current.ToInternalValue(); 1130} 1131 1132File* EntryImpl::GetBackingFile(Addr address, int index) { 1133 File* file; 1134 if (address.is_separate_file()) 1135 file = GetExternalFile(address, index); 1136 else 1137 file = backend_->File(address); 1138 return file; 1139} 1140 1141File* EntryImpl::GetExternalFile(Addr address, int index) { 1142 DCHECK(index >= 0 && index <= kKeyFileIndex); 1143 if (!files_[index].get()) { 1144 // For a key file, use mixed mode IO. 1145 scoped_refptr<File> file(new File(kKeyFileIndex == index)); 1146 if (file->Init(backend_->GetFileName(address))) 1147 files_[index].swap(file); 1148 } 1149 return files_[index].get(); 1150} 1151 1152// We keep a memory buffer for everything that ends up stored on a block file 1153// (because we don't know yet the final data size), and for some of the data 1154// that end up on external files. This function will initialize that memory 1155// buffer and / or the files needed to store the data. 1156// 1157// In general, a buffer may overlap data already stored on disk, and in that 1158// case, the contents of the buffer are the most accurate. It may also extend 1159// the file, but we don't want to read from disk just to keep the buffer up to 1160// date. This means that as soon as there is a chance to get confused about what 1161// is the most recent version of some part of a file, we'll flush the buffer and 1162// reuse it for the new data. Keep in mind that the normal use pattern is quite 1163// simple (write sequentially from the beginning), so we optimize for handling 1164// that case. 1165bool EntryImpl::PrepareTarget(int index, int offset, int buf_len, 1166 bool truncate) { 1167 if (truncate) 1168 return HandleTruncation(index, offset, buf_len); 1169 1170 if (!offset && !buf_len) 1171 return true; 1172 1173 Addr address(entry_.Data()->data_addr[index]); 1174 if (address.is_initialized()) { 1175 if (address.is_block_file() && !MoveToLocalBuffer(index)) 1176 return false; 1177 1178 if (!user_buffers_[index].get() && offset < kMaxBlockSize) { 1179 // We are about to create a buffer for the first 16KB, make sure that we 1180 // preserve existing data. 1181 if (!CopyToLocalBuffer(index)) 1182 return false; 1183 } 1184 } 1185 1186 if (!user_buffers_[index].get()) 1187 user_buffers_[index].reset(new UserBuffer(backend_)); 1188 1189 return PrepareBuffer(index, offset, buf_len); 1190} 1191 1192// We get to this function with some data already stored. If there is a 1193// truncation that results on data stored internally, we'll explicitly 1194// handle the case here. 1195bool EntryImpl::HandleTruncation(int index, int offset, int buf_len) { 1196 Addr address(entry_.Data()->data_addr[index]); 1197 1198 int current_size = entry_.Data()->data_size[index]; 1199 int new_size = offset + buf_len; 1200 1201 if (!new_size) { 1202 // This is by far the most common scenario. 1203 backend_->ModifyStorageSize(current_size - unreported_size_[index], 0); 1204 entry_.Data()->data_addr[index] = 0; 1205 entry_.Data()->data_size[index] = 0; 1206 unreported_size_[index] = 0; 1207 entry_.Store(); 1208 DeleteData(address, index); 1209 1210 user_buffers_[index].reset(); 1211 return true; 1212 } 1213 1214 // We never postpone truncating a file, if there is one, but we may postpone 1215 // telling the backend about the size reduction. 1216 if (user_buffers_[index].get()) { 1217 DCHECK_GE(current_size, user_buffers_[index]->Start()); 1218 if (!address.is_initialized()) { 1219 // There is no overlap between the buffer and disk. 1220 if (new_size > user_buffers_[index]->Start()) { 1221 // Just truncate our buffer. 1222 DCHECK_LT(new_size, user_buffers_[index]->End()); 1223 user_buffers_[index]->Truncate(new_size); 1224 return true; 1225 } 1226 1227 // Just discard our buffer. 1228 user_buffers_[index]->Reset(); 1229 return PrepareBuffer(index, offset, buf_len); 1230 } 1231 1232 // There is some overlap or we need to extend the file before the 1233 // truncation. 1234 if (offset > user_buffers_[index]->Start()) 1235 user_buffers_[index]->Truncate(new_size); 1236 UpdateSize(index, current_size, new_size); 1237 if (!Flush(index, 0)) 1238 return false; 1239 user_buffers_[index].reset(); 1240 } 1241 1242 // We have data somewhere, and it is not in a buffer. 1243 DCHECK(!user_buffers_[index].get()); 1244 DCHECK(address.is_initialized()); 1245 1246 if (new_size > kMaxBlockSize) 1247 return true; // Let the operation go directly to disk. 1248 1249 return ImportSeparateFile(index, offset + buf_len); 1250} 1251 1252bool EntryImpl::CopyToLocalBuffer(int index) { 1253 Addr address(entry_.Data()->data_addr[index]); 1254 DCHECK(!user_buffers_[index].get()); 1255 DCHECK(address.is_initialized()); 1256 1257 int len = std::min(entry_.Data()->data_size[index], kMaxBlockSize); 1258 user_buffers_[index].reset(new UserBuffer(backend_)); 1259 user_buffers_[index]->Write(len, NULL, 0); 1260 1261 File* file = GetBackingFile(address, index); 1262 int offset = 0; 1263 1264 if (address.is_block_file()) 1265 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; 1266 1267 if (!file || 1268 !file->Read(user_buffers_[index]->Data(), len, offset, NULL, NULL)) { 1269 user_buffers_[index].reset(); 1270 return false; 1271 } 1272 return true; 1273} 1274 1275bool EntryImpl::MoveToLocalBuffer(int index) { 1276 if (!CopyToLocalBuffer(index)) 1277 return false; 1278 1279 Addr address(entry_.Data()->data_addr[index]); 1280 entry_.Data()->data_addr[index] = 0; 1281 entry_.Store(); 1282 DeleteData(address, index); 1283 1284 // If we lose this entry we'll see it as zero sized. 1285 int len = entry_.Data()->data_size[index]; 1286 backend_->ModifyStorageSize(len - unreported_size_[index], 0); 1287 unreported_size_[index] = len; 1288 return true; 1289} 1290 1291bool EntryImpl::ImportSeparateFile(int index, int new_size) { 1292 if (entry_.Data()->data_size[index] > new_size) 1293 UpdateSize(index, entry_.Data()->data_size[index], new_size); 1294 1295 return MoveToLocalBuffer(index); 1296} 1297 1298bool EntryImpl::PrepareBuffer(int index, int offset, int buf_len) { 1299 DCHECK(user_buffers_[index].get()); 1300 if ((user_buffers_[index]->End() && offset > user_buffers_[index]->End()) || 1301 offset > entry_.Data()->data_size[index]) { 1302 // We are about to extend the buffer or the file (with zeros), so make sure 1303 // that we are not overwriting anything. 1304 Addr address(entry_.Data()->data_addr[index]); 1305 if (address.is_initialized() && address.is_separate_file()) { 1306 if (!Flush(index, 0)) 1307 return false; 1308 // There is an actual file already, and we don't want to keep track of 1309 // its length so we let this operation go straight to disk. 1310 // The only case when a buffer is allowed to extend the file (as in fill 1311 // with zeros before the start) is when there is no file yet to extend. 1312 user_buffers_[index].reset(); 1313 return true; 1314 } 1315 } 1316 1317 if (!user_buffers_[index]->PreWrite(offset, buf_len)) { 1318 if (!Flush(index, offset + buf_len)) 1319 return false; 1320 1321 // Lets try again. 1322 if (offset > user_buffers_[index]->End() || 1323 !user_buffers_[index]->PreWrite(offset, buf_len)) { 1324 // We cannot complete the operation with a buffer. 1325 DCHECK(!user_buffers_[index]->Size()); 1326 DCHECK(!user_buffers_[index]->Start()); 1327 user_buffers_[index].reset(); 1328 } 1329 } 1330 return true; 1331} 1332 1333bool EntryImpl::Flush(int index, int min_len) { 1334 Addr address(entry_.Data()->data_addr[index]); 1335 DCHECK(user_buffers_[index].get()); 1336 DCHECK(!address.is_initialized() || address.is_separate_file()); 1337 DVLOG(3) << "Flush"; 1338 1339 int size = std::max(entry_.Data()->data_size[index], min_len); 1340 if (size && !address.is_initialized() && !CreateDataBlock(index, size)) 1341 return false; 1342 1343 if (!entry_.Data()->data_size[index]) { 1344 DCHECK(!user_buffers_[index]->Size()); 1345 return true; 1346 } 1347 1348 address.set_value(entry_.Data()->data_addr[index]); 1349 1350 int len = user_buffers_[index]->Size(); 1351 int offset = user_buffers_[index]->Start(); 1352 if (!len && !offset) 1353 return true; 1354 1355 if (address.is_block_file()) { 1356 DCHECK_EQ(len, entry_.Data()->data_size[index]); 1357 DCHECK(!offset); 1358 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize; 1359 } 1360 1361 File* file = GetBackingFile(address, index); 1362 if (!file) 1363 return false; 1364 1365 if (!file->Write(user_buffers_[index]->Data(), len, offset, NULL, NULL)) 1366 return false; 1367 user_buffers_[index]->Reset(); 1368 1369 return true; 1370} 1371 1372void EntryImpl::UpdateSize(int index, int old_size, int new_size) { 1373 if (entry_.Data()->data_size[index] == new_size) 1374 return; 1375 1376 unreported_size_[index] += new_size - old_size; 1377 entry_.Data()->data_size[index] = new_size; 1378 entry_.set_modified(); 1379} 1380 1381int EntryImpl::InitSparseData() { 1382 if (sparse_.get()) 1383 return net::OK; 1384 1385 // Use a local variable so that sparse_ never goes from 'valid' to NULL. 1386 scoped_ptr<SparseControl> sparse(new SparseControl(this)); 1387 int result = sparse->Init(); 1388 if (net::OK == result) 1389 sparse_.swap(sparse); 1390 1391 return result; 1392} 1393 1394void EntryImpl::SetEntryFlags(uint32 flags) { 1395 entry_.Data()->flags |= flags; 1396 entry_.set_modified(); 1397} 1398 1399uint32 EntryImpl::GetEntryFlags() { 1400 return entry_.Data()->flags; 1401} 1402 1403void EntryImpl::GetData(int index, char** buffer, Addr* address) { 1404 if (user_buffers_[index].get() && user_buffers_[index]->Size() && 1405 !user_buffers_[index]->Start()) { 1406 // The data is already in memory, just copy it and we're done. 1407 int data_len = entry_.Data()->data_size[index]; 1408 if (data_len <= user_buffers_[index]->Size()) { 1409 DCHECK(!user_buffers_[index]->Start()); 1410 *buffer = new char[data_len]; 1411 memcpy(*buffer, user_buffers_[index]->Data(), data_len); 1412 return; 1413 } 1414 } 1415 1416 // Bad news: we'd have to read the info from disk so instead we'll just tell 1417 // the caller where to read from. 1418 *buffer = NULL; 1419 address->set_value(entry_.Data()->data_addr[index]); 1420 if (address->is_initialized()) { 1421 // Prevent us from deleting the block from the backing store. 1422 backend_->ModifyStorageSize(entry_.Data()->data_size[index] - 1423 unreported_size_[index], 0); 1424 entry_.Data()->data_addr[index] = 0; 1425 entry_.Data()->data_size[index] = 0; 1426 } 1427} 1428 1429void EntryImpl::Log(const char* msg) { 1430 int dirty = 0; 1431 if (node_.HasData()) { 1432 dirty = node_.Data()->dirty; 1433 } 1434 1435 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), 1436 entry_.address().value(), node_.address().value()); 1437 1438 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], 1439 entry_.Data()->data_addr[1], entry_.Data()->long_key); 1440 1441 Trace(" doomed: %d 0x%x", doomed_, dirty); 1442} 1443 1444} // namespace disk_cache 1445