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