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