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