OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/sparse_control.h" | 5 #include "net/disk_cache/sparse_control.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; | 97 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; |
98 children_map_.Resize(num_bits, false); | 98 children_map_.Resize(num_bits, false); |
99 children_map_.SetMap(data->bitmap, num_bits / 32); | 99 children_map_.SetMap(data->bitmap, num_bits / 32); |
100 buffer_.reset(); | 100 buffer_.reset(); |
101 | 101 |
102 DeleteChildren(); | 102 DeleteChildren(); |
103 } | 103 } |
104 | 104 |
105 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { | 105 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { |
106 DCHECK(address.is_block_file()); | 106 DCHECK(address.is_block_file()); |
107 if (!backend_.get()) | 107 if (!backend_) |
108 return Release(); | 108 return Release(); |
109 | 109 |
110 disk_cache::File* file(backend_->File(address)); | 110 disk_cache::File* file(backend_->File(address)); |
111 if (!file) | 111 if (!file) |
112 return Release(); | 112 return Release(); |
113 | 113 |
114 size_t file_offset = address.start_block() * address.BlockSize() + | 114 size_t file_offset = address.start_block() * address.BlockSize() + |
115 disk_cache::kBlockHeaderSize; | 115 disk_cache::kBlockHeaderSize; |
116 | 116 |
117 buffer_.reset(new char[len]); | 117 buffer_.reset(new char[len]); |
118 bool completed; | 118 bool completed; |
119 if (!file->Read(buffer_.get(), len, file_offset, this, &completed)) | 119 if (!file->Read(buffer_.get(), len, file_offset, this, &completed)) |
120 return Release(); | 120 return Release(); |
121 | 121 |
122 if (completed) | 122 if (completed) |
123 OnFileIOComplete(len); | 123 OnFileIOComplete(len); |
124 | 124 |
125 // And wait until OnFileIOComplete gets called. | 125 // And wait until OnFileIOComplete gets called. |
126 } | 126 } |
127 | 127 |
128 void ChildrenDeleter::DeleteChildren() { | 128 void ChildrenDeleter::DeleteChildren() { |
129 int child_id = 0; | 129 int child_id = 0; |
130 if (!children_map_.FindNextSetBit(&child_id) || !backend_.get()) { | 130 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { |
131 // We are done. Just delete this object. | 131 // We are done. Just delete this object. |
132 return Release(); | 132 return Release(); |
133 } | 133 } |
134 std::string child_name = GenerateChildName(name_, signature_, child_id); | 134 std::string child_name = GenerateChildName(name_, signature_, child_id); |
135 backend_->SyncDoomEntry(child_name); | 135 backend_->SyncDoomEntry(child_name); |
136 children_map_.Set(child_id, false); | 136 children_map_.Set(child_id, false); |
137 | 137 |
138 // Post a task to delete the next child. | 138 // Post a task to delete the next child. |
139 base::MessageLoop::current()->PostTask( | 139 base::MessageLoop::current()->PostTask( |
140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); | 140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); |
141 } | 141 } |
142 | 142 |
| 143 // ----------------------------------------------------------------------- |
| 144 |
143 // Returns the NetLog event type corresponding to a SparseOperation. | 145 // Returns the NetLog event type corresponding to a SparseOperation. |
144 net::NetLog::EventType GetSparseEventType( | 146 net::NetLog::EventType GetSparseEventType( |
145 disk_cache::SparseControl::SparseOperation operation) { | 147 disk_cache::SparseControl::SparseOperation operation) { |
146 switch (operation) { | 148 switch (operation) { |
147 case disk_cache::SparseControl::kReadOperation: | 149 case disk_cache::SparseControl::kReadOperation: |
148 return net::NetLog::TYPE_SPARSE_READ; | 150 return net::NetLog::TYPE_SPARSE_READ; |
149 case disk_cache::SparseControl::kWriteOperation: | 151 case disk_cache::SparseControl::kWriteOperation: |
150 return net::NetLog::TYPE_SPARSE_WRITE; | 152 return net::NetLog::TYPE_SPARSE_WRITE; |
151 case disk_cache::SparseControl::kGetRangeOperation: | 153 case disk_cache::SparseControl::kGetRangeOperation: |
152 return net::NetLog::TYPE_SPARSE_GET_RANGE; | 154 return net::NetLog::TYPE_SPARSE_GET_RANGE; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 memset(&child_data_, 0, sizeof(child_data_)); | 205 memset(&child_data_, 0, sizeof(child_data_)); |
204 } | 206 } |
205 | 207 |
206 SparseControl::~SparseControl() { | 208 SparseControl::~SparseControl() { |
207 if (child_) | 209 if (child_) |
208 CloseChild(); | 210 CloseChild(); |
209 if (init_) | 211 if (init_) |
210 WriteSparseData(); | 212 WriteSparseData(); |
211 } | 213 } |
212 | 214 |
213 int SparseControl::Init() { | |
214 DCHECK(!init_); | |
215 | |
216 // We should not have sparse data for the exposed entry. | |
217 if (entry_->GetDataSize(kSparseData)) | |
218 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | |
219 | |
220 // Now see if there is something where we store our data. | |
221 int rv = net::OK; | |
222 int data_len = entry_->GetDataSize(kSparseIndex); | |
223 if (!data_len) { | |
224 rv = CreateSparseEntry(); | |
225 } else { | |
226 rv = OpenSparseEntry(data_len); | |
227 } | |
228 | |
229 if (rv == net::OK) | |
230 init_ = true; | |
231 return rv; | |
232 } | |
233 | |
234 bool SparseControl::CouldBeSparse() const { | 215 bool SparseControl::CouldBeSparse() const { |
235 DCHECK(!init_); | 216 DCHECK(!init_); |
236 | 217 |
237 if (entry_->GetDataSize(kSparseData)) | 218 if (entry_->GetDataSize(kSparseData)) |
238 return false; | 219 return false; |
239 | 220 |
240 // We don't verify the data, just see if it could be there. | 221 // We don't verify the data, just see if it could be there. |
241 return (entry_->GetDataSize(kSparseIndex) != 0); | 222 return (entry_->GetDataSize(kSparseIndex) != 0); |
242 } | 223 } |
243 | 224 |
244 int SparseControl::StartIO(SparseOperation op, int64 offset, net::IOBuffer* buf, | 225 int SparseControl::StartIO(SparseOperation op, int64 offset, net::IOBuffer* buf, |
245 int buf_len, const CompletionCallback& callback) { | 226 int buf_len, const CompletionCallback& callback) { |
246 DCHECK(init_); | 227 DCHECK(init_); |
247 // We don't support simultaneous IO for sparse data. | 228 // We don't support simultaneous IO for sparse data. |
248 if (operation_ != kNoOperation) | 229 if (operation_ != kNoOperation) |
249 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 230 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
250 | 231 |
251 if (offset < 0 || buf_len < 0) | 232 if (offset < 0 || buf_len < 0) |
252 return net::ERR_INVALID_ARGUMENT; | 233 return net::ERR_INVALID_ARGUMENT; |
253 | 234 |
254 // We only support up to 64 GB. | 235 // We only support up to 64 GB. |
255 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) | 236 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) |
256 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 237 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
257 | 238 |
258 DCHECK(!user_buf_.get()); | 239 DCHECK(!user_buf_); |
259 DCHECK(user_callback_.is_null()); | 240 DCHECK(user_callback_.is_null()); |
260 | 241 |
261 if (!buf && (op == kReadOperation || op == kWriteOperation)) | 242 if (!buf && (op == kReadOperation || op == kWriteOperation)) |
262 return 0; | 243 return 0; |
263 | 244 |
264 // Copy the operation parameters. | 245 // Copy the operation parameters. |
265 operation_ = op; | 246 operation_ = op; |
266 offset_ = offset; | 247 offset_ = offset; |
267 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 248 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
268 buf_len_ = buf_len; | 249 buf_len_ = buf_len; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 return; | 324 return; |
344 | 325 |
345 char* buffer; | 326 char* buffer; |
346 Addr address; | 327 Addr address; |
347 entry->GetData(kSparseIndex, &buffer, &address); | 328 entry->GetData(kSparseIndex, &buffer, &address); |
348 if (!buffer && !address.is_initialized()) | 329 if (!buffer && !address.is_initialized()) |
349 return; | 330 return; |
350 | 331 |
351 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); | 332 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); |
352 | 333 |
353 DCHECK(entry->backend_.get()); | 334 DCHECK(entry->backend_); |
354 ChildrenDeleter* deleter = | 335 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(), |
355 new ChildrenDeleter(entry->backend_.get(), entry->GetKey()); | 336 entry->GetKey()); |
356 // The object will self destruct when finished. | 337 // The object will self destruct when finished. |
357 deleter->AddRef(); | 338 deleter->AddRef(); |
358 | 339 |
359 if (buffer) { | 340 if (buffer) { |
360 base::MessageLoop::current()->PostTask( | 341 base::MessageLoop::current()->PostTask( |
361 FROM_HERE, | 342 FROM_HERE, |
362 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); | 343 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); |
363 } else { | 344 } else { |
364 base::MessageLoop::current()->PostTask( | 345 base::MessageLoop::current()->PostTask( |
365 FROM_HERE, | 346 FROM_HERE, |
366 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); | 347 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); |
367 } | 348 } |
368 } | 349 } |
369 | 350 |
| 351 // ----------------------------------------------------------------------- |
| 352 |
| 353 int SparseControl::Init() { |
| 354 DCHECK(!init_); |
| 355 |
| 356 // We should not have sparse data for the exposed entry. |
| 357 if (entry_->GetDataSize(kSparseData)) |
| 358 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 359 |
| 360 // Now see if there is something where we store our data. |
| 361 int rv = net::OK; |
| 362 int data_len = entry_->GetDataSize(kSparseIndex); |
| 363 if (!data_len) { |
| 364 rv = CreateSparseEntry(); |
| 365 } else { |
| 366 rv = OpenSparseEntry(data_len); |
| 367 } |
| 368 |
| 369 if (rv == net::OK) |
| 370 init_ = true; |
| 371 return rv; |
| 372 } |
| 373 |
370 // We are going to start using this entry to store sparse data, so we have to | 374 // We are going to start using this entry to store sparse data, so we have to |
371 // initialize our control info. | 375 // initialize our control info. |
372 int SparseControl::CreateSparseEntry() { | 376 int SparseControl::CreateSparseEntry() { |
373 if (CHILD_ENTRY & entry_->GetEntryFlags()) | 377 if (CHILD_ENTRY & entry_->GetEntryFlags()) |
374 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 378 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
375 | 379 |
376 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 380 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
377 sparse_header_.signature = Time::Now().ToInternalValue(); | 381 sparse_header_.signature = Time::Now().ToInternalValue(); |
378 sparse_header_.magic = kIndexMagic; | 382 sparse_header_.magic = kIndexMagic; |
379 sparse_header_.parent_key_len = entry_->GetKey().size(); | 383 sparse_header_.parent_key_len = entry_->GetKey().size(); |
380 children_map_.Resize(kNumSparseBits, true); | 384 children_map_.Resize(kNumSparseBits, true); |
381 | 385 |
382 // Save the header. The bitmap is saved in the destructor. | 386 // Save the header. The bitmap is saved in the destructor. |
383 scoped_refptr<net::IOBuffer> buf( | 387 scoped_refptr<net::IOBuffer> buf( |
384 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 388 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
385 | 389 |
386 int rv = entry_->WriteData(kSparseIndex, | 390 int rv = entry_->WriteData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), |
387 0, | 391 CompletionCallback(), false); |
388 buf.get(), | |
389 sizeof(sparse_header_), | |
390 CompletionCallback(), | |
391 false); | |
392 if (rv != sizeof(sparse_header_)) { | 392 if (rv != sizeof(sparse_header_)) { |
393 DLOG(ERROR) << "Unable to save sparse_header_"; | 393 DLOG(ERROR) << "Unable to save sparse_header_"; |
394 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 394 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
395 } | 395 } |
396 | 396 |
397 entry_->SetEntryFlags(PARENT_ENTRY); | 397 entry_->SetEntryFlags(PARENT_ENTRY); |
398 return net::OK; | 398 return net::OK; |
399 } | 399 } |
400 | 400 |
401 // We are opening an entry from disk. Make sure that our control data is there. | 401 // We are opening an entry from disk. Make sure that our control data is there. |
402 int SparseControl::OpenSparseEntry(int data_len) { | 402 int SparseControl::OpenSparseEntry(int data_len) { |
403 if (data_len < static_cast<int>(sizeof(SparseData))) | 403 if (data_len < static_cast<int>(sizeof(SparseData))) |
404 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 404 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
405 | 405 |
406 if (entry_->GetDataSize(kSparseData)) | 406 if (entry_->GetDataSize(kSparseData)) |
407 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 407 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
408 | 408 |
409 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) | 409 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) |
410 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 410 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
411 | 411 |
412 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. | 412 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. |
413 int map_len = data_len - sizeof(sparse_header_); | 413 int map_len = data_len - sizeof(sparse_header_); |
414 if (map_len > kMaxMapSize || map_len % 4) | 414 if (map_len > kMaxMapSize || map_len % 4) |
415 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 415 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
416 | 416 |
417 scoped_refptr<net::IOBuffer> buf( | 417 scoped_refptr<net::IOBuffer> buf( |
418 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 418 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
419 | 419 |
420 // Read header. | 420 // Read header. |
421 int rv = entry_->ReadData( | 421 int rv = entry_->ReadData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), |
422 kSparseIndex, 0, buf.get(), sizeof(sparse_header_), CompletionCallback()); | 422 CompletionCallback()); |
423 if (rv != static_cast<int>(sizeof(sparse_header_))) | 423 if (rv != static_cast<int>(sizeof(sparse_header_))) |
424 return net::ERR_CACHE_READ_FAILURE; | 424 return net::ERR_CACHE_READ_FAILURE; |
425 | 425 |
426 // The real validation should be performed by the caller. This is just to | 426 // The real validation should be performed by the caller. This is just to |
427 // double check. | 427 // double check. |
428 if (sparse_header_.magic != kIndexMagic || | 428 if (sparse_header_.magic != kIndexMagic || |
429 sparse_header_.parent_key_len != | 429 sparse_header_.parent_key_len != |
430 static_cast<int>(entry_->GetKey().size())) | 430 static_cast<int>(entry_->GetKey().size())) |
431 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 431 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
432 | 432 |
433 // Read the actual bitmap. | 433 // Read the actual bitmap. |
434 buf = new net::IOBuffer(map_len); | 434 buf = new net::IOBuffer(map_len); |
435 rv = entry_->ReadData(kSparseIndex, | 435 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
436 sizeof(sparse_header_), | 436 map_len, CompletionCallback()); |
437 buf.get(), | |
438 map_len, | |
439 CompletionCallback()); | |
440 if (rv != map_len) | 437 if (rv != map_len) |
441 return net::ERR_CACHE_READ_FAILURE; | 438 return net::ERR_CACHE_READ_FAILURE; |
442 | 439 |
443 // Grow the bitmap to the current size and copy the bits. | 440 // Grow the bitmap to the current size and copy the bits. |
444 children_map_.Resize(map_len * 8, false); | 441 children_map_.Resize(map_len * 8, false); |
445 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); | 442 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); |
446 return net::OK; | 443 return net::OK; |
447 } | 444 } |
448 | 445 |
449 bool SparseControl::OpenChild() { | 446 bool SparseControl::OpenChild() { |
450 DCHECK_GE(result_, 0); | 447 DCHECK_GE(result_, 0); |
451 | 448 |
452 std::string key = GenerateChildKey(); | 449 std::string key = GenerateChildKey(); |
453 if (child_) { | 450 if (child_) { |
454 // Keep using the same child or open another one?. | 451 // Keep using the same child or open another one?. |
455 if (key == child_->GetKey()) | 452 if (key == child_->GetKey()) |
456 return true; | 453 return true; |
457 CloseChild(); | 454 CloseChild(); |
458 } | 455 } |
459 | 456 |
460 // See if we are tracking this child. | 457 // See if we are tracking this child. |
461 if (!ChildPresent()) | 458 if (!ChildPresent()) |
462 return ContinueWithoutChild(key); | 459 return ContinueWithoutChild(key); |
463 | 460 |
464 if (!entry_->backend_.get()) | 461 if (!entry_->backend_) |
465 return false; | 462 return false; |
466 | 463 |
467 child_ = entry_->backend_->OpenEntryImpl(key); | 464 child_ = entry_->backend_->OpenEntryImpl(key); |
468 if (!child_) | 465 if (!child_) |
469 return ContinueWithoutChild(key); | 466 return ContinueWithoutChild(key); |
470 | 467 |
471 EntryImpl* child = static_cast<EntryImpl*>(child_); | 468 EntryImpl* child = static_cast<EntryImpl*>(child_); |
472 if (!(CHILD_ENTRY & child->GetEntryFlags()) || | 469 if (!(CHILD_ENTRY & child->GetEntryFlags()) || |
473 child->GetDataSize(kSparseIndex) < | 470 child->GetDataSize(kSparseIndex) < |
474 static_cast<int>(sizeof(child_data_))) | 471 static_cast<int>(sizeof(child_data_))) |
475 return KillChildAndContinue(key, false); | 472 return KillChildAndContinue(key, false); |
476 | 473 |
477 scoped_refptr<net::WrappedIOBuffer> buf( | 474 scoped_refptr<net::WrappedIOBuffer> buf( |
478 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 475 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
479 | 476 |
480 // Read signature. | 477 // Read signature. |
481 int rv = child_->ReadData( | 478 int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
482 kSparseIndex, 0, buf.get(), sizeof(child_data_), CompletionCallback()); | 479 CompletionCallback()); |
483 if (rv != sizeof(child_data_)) | 480 if (rv != sizeof(child_data_)) |
484 return KillChildAndContinue(key, true); // This is a fatal failure. | 481 return KillChildAndContinue(key, true); // This is a fatal failure. |
485 | 482 |
486 if (child_data_.header.signature != sparse_header_.signature || | 483 if (child_data_.header.signature != sparse_header_.signature || |
487 child_data_.header.magic != kIndexMagic) | 484 child_data_.header.magic != kIndexMagic) |
488 return KillChildAndContinue(key, false); | 485 return KillChildAndContinue(key, false); |
489 | 486 |
490 if (child_data_.header.last_block_len < 0 || | 487 if (child_data_.header.last_block_len < 0 || |
491 child_data_.header.last_block_len > kBlockSize) { | 488 child_data_.header.last_block_len > kBlockSize) { |
492 // Make sure these values are always within range. | 489 // Make sure these values are always within range. |
493 child_data_.header.last_block_len = 0; | 490 child_data_.header.last_block_len = 0; |
494 child_data_.header.last_block = -1; | 491 child_data_.header.last_block = -1; |
495 } | 492 } |
496 | 493 |
497 return true; | 494 return true; |
498 } | 495 } |
499 | 496 |
500 void SparseControl::CloseChild() { | 497 void SparseControl::CloseChild() { |
501 scoped_refptr<net::WrappedIOBuffer> buf( | 498 scoped_refptr<net::WrappedIOBuffer> buf( |
502 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 499 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
503 | 500 |
504 // Save the allocation bitmap before closing the child entry. | 501 // Save the allocation bitmap before closing the child entry. |
505 int rv = child_->WriteData(kSparseIndex, | 502 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
506 0, | |
507 buf.get(), | |
508 sizeof(child_data_), | |
509 CompletionCallback(), | 503 CompletionCallback(), |
510 false); | 504 false); |
511 if (rv != sizeof(child_data_)) | 505 if (rv != sizeof(child_data_)) |
512 DLOG(ERROR) << "Failed to save child data"; | 506 DLOG(ERROR) << "Failed to save child data"; |
513 child_->Release(); | 507 child_->Release(); |
514 child_ = NULL; | 508 child_ = NULL; |
515 } | 509 } |
516 | 510 |
517 std::string SparseControl::GenerateChildKey() { | |
518 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, | |
519 offset_ >> 20); | |
520 } | |
521 | |
522 // We are deleting the child because something went wrong. | |
523 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) { | |
524 SetChildBit(false); | |
525 child_->DoomImpl(); | |
526 child_->Release(); | |
527 child_ = NULL; | |
528 if (fatal) { | |
529 result_ = net::ERR_CACHE_READ_FAILURE; | |
530 return false; | |
531 } | |
532 return ContinueWithoutChild(key); | |
533 } | |
534 | |
535 // We were not able to open this child; see what we can do. | 511 // We were not able to open this child; see what we can do. |
536 bool SparseControl::ContinueWithoutChild(const std::string& key) { | 512 bool SparseControl::ContinueWithoutChild(const std::string& key) { |
537 if (kReadOperation == operation_) | 513 if (kReadOperation == operation_) |
538 return false; | 514 return false; |
539 if (kGetRangeOperation == operation_) | 515 if (kGetRangeOperation == operation_) |
540 return true; | 516 return true; |
541 | 517 |
542 if (!entry_->backend_.get()) | 518 if (!entry_->backend_) |
543 return false; | 519 return false; |
544 | 520 |
545 child_ = entry_->backend_->CreateEntryImpl(key); | 521 child_ = entry_->backend_->CreateEntryImpl(key); |
546 if (!child_) { | 522 if (!child_) { |
547 child_ = NULL; | 523 child_ = NULL; |
548 result_ = net::ERR_CACHE_READ_FAILURE; | 524 result_ = net::ERR_CACHE_READ_FAILURE; |
549 return false; | 525 return false; |
550 } | 526 } |
551 // Write signature. | 527 // Write signature. |
552 InitChildData(); | 528 InitChildData(); |
553 return true; | 529 return true; |
554 } | 530 } |
555 | 531 |
| 532 void SparseControl::WriteSparseData() { |
| 533 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( |
| 534 reinterpret_cast<const char*>(children_map_.GetMap()))); |
| 535 |
| 536 int len = children_map_.ArraySize() * 4; |
| 537 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
| 538 len, CompletionCallback(), false); |
| 539 if (rv != len) { |
| 540 DLOG(ERROR) << "Unable to save sparse map"; |
| 541 } |
| 542 } |
| 543 |
| 544 bool SparseControl::DoChildIO() { |
| 545 finished_ = true; |
| 546 if (!buf_len_ || result_ < 0) |
| 547 return false; |
| 548 |
| 549 if (!OpenChild()) |
| 550 return false; |
| 551 |
| 552 if (!VerifyRange()) |
| 553 return false; |
| 554 |
| 555 // We have more work to do. Let's not trigger a callback to the caller. |
| 556 finished_ = false; |
| 557 CompletionCallback callback; |
| 558 if (!user_callback_.is_null()) { |
| 559 callback = |
| 560 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); |
| 561 } |
| 562 |
| 563 int rv = 0; |
| 564 switch (operation_) { |
| 565 case kReadOperation: |
| 566 if (entry_->net_log().IsLoggingAllEvents()) { |
| 567 entry_->net_log().BeginEvent( |
| 568 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 569 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 570 child_len_)); |
| 571 } |
| 572 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(), |
| 573 child_len_, callback); |
| 574 break; |
| 575 case kWriteOperation: |
| 576 if (entry_->net_log().IsLoggingAllEvents()) { |
| 577 entry_->net_log().BeginEvent( |
| 578 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 579 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 580 child_len_)); |
| 581 } |
| 582 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(), |
| 583 child_len_, callback, false); |
| 584 break; |
| 585 case kGetRangeOperation: |
| 586 rv = DoGetAvailableRange(); |
| 587 break; |
| 588 default: |
| 589 NOTREACHED(); |
| 590 } |
| 591 |
| 592 if (rv == net::ERR_IO_PENDING) { |
| 593 if (!pending_) { |
| 594 pending_ = true; |
| 595 // The child will protect himself against closing the entry while IO is in |
| 596 // progress. However, this entry can still be closed, and that would not |
| 597 // be a good thing for us, so we increase the refcount until we're |
| 598 // finished doing sparse stuff. |
| 599 entry_->AddRef(); // Balanced in DoUserCallback. |
| 600 } |
| 601 return false; |
| 602 } |
| 603 if (!rv) |
| 604 return false; |
| 605 |
| 606 DoChildIOCompleted(rv); |
| 607 return true; |
| 608 } |
| 609 |
| 610 void SparseControl::DoChildIOCompleted(int result) { |
| 611 LogChildOperationEnd(entry_->net_log(), operation_, result); |
| 612 if (result < 0) { |
| 613 // We fail the whole operation if we encounter an error. |
| 614 result_ = result; |
| 615 return; |
| 616 } |
| 617 |
| 618 UpdateRange(result); |
| 619 |
| 620 result_ += result; |
| 621 offset_ += result; |
| 622 buf_len_ -= result; |
| 623 |
| 624 // We'll be reusing the user provided buffer for the next chunk. |
| 625 if (buf_len_ && user_buf_) |
| 626 user_buf_->DidConsume(result); |
| 627 } |
| 628 |
| 629 std::string SparseControl::GenerateChildKey() { |
| 630 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, |
| 631 offset_ >> 20); |
| 632 } |
| 633 |
| 634 // We are deleting the child because something went wrong. |
| 635 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) { |
| 636 SetChildBit(false); |
| 637 child_->DoomImpl(); |
| 638 child_->Release(); |
| 639 child_ = NULL; |
| 640 if (fatal) { |
| 641 result_ = net::ERR_CACHE_READ_FAILURE; |
| 642 return false; |
| 643 } |
| 644 return ContinueWithoutChild(key); |
| 645 } |
| 646 |
556 bool SparseControl::ChildPresent() { | 647 bool SparseControl::ChildPresent() { |
557 int child_bit = static_cast<int>(offset_ >> 20); | 648 int child_bit = static_cast<int>(offset_ >> 20); |
558 if (children_map_.Size() <= child_bit) | 649 if (children_map_.Size() <= child_bit) |
559 return false; | 650 return false; |
560 | 651 |
561 return children_map_.Get(child_bit); | 652 return children_map_.Get(child_bit); |
562 } | 653 } |
563 | 654 |
564 void SparseControl::SetChildBit(bool value) { | 655 void SparseControl::SetChildBit(bool value) { |
565 int child_bit = static_cast<int>(offset_ >> 20); | 656 int child_bit = static_cast<int>(offset_ >> 20); |
566 | 657 |
567 // We may have to increase the bitmap of child entries. | 658 // We may have to increase the bitmap of child entries. |
568 if (children_map_.Size() <= child_bit) | 659 if (children_map_.Size() <= child_bit) |
569 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); | 660 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); |
570 | 661 |
571 children_map_.Set(child_bit, value); | 662 children_map_.Set(child_bit, value); |
572 } | 663 } |
573 | 664 |
574 void SparseControl::WriteSparseData() { | |
575 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( | |
576 reinterpret_cast<const char*>(children_map_.GetMap()))); | |
577 | |
578 int len = children_map_.ArraySize() * 4; | |
579 int rv = entry_->WriteData(kSparseIndex, | |
580 sizeof(sparse_header_), | |
581 buf.get(), | |
582 len, | |
583 CompletionCallback(), | |
584 false); | |
585 if (rv != len) { | |
586 DLOG(ERROR) << "Unable to save sparse map"; | |
587 } | |
588 } | |
589 | |
590 bool SparseControl::VerifyRange() { | 665 bool SparseControl::VerifyRange() { |
591 DCHECK_GE(result_, 0); | 666 DCHECK_GE(result_, 0); |
592 | 667 |
593 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); | 668 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); |
594 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); | 669 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); |
595 | 670 |
596 // We can write to (or get info from) anywhere in this child. | 671 // We can write to (or get info from) anywhere in this child. |
597 if (operation_ != kReadOperation) | 672 if (operation_ != kReadOperation) |
598 return true; | 673 return true; |
599 | 674 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 // We know the real type of child_. | 752 // We know the real type of child_. |
678 EntryImpl* child = static_cast<EntryImpl*>(child_); | 753 EntryImpl* child = static_cast<EntryImpl*>(child_); |
679 child->SetEntryFlags(CHILD_ENTRY); | 754 child->SetEntryFlags(CHILD_ENTRY); |
680 | 755 |
681 memset(&child_data_, 0, sizeof(child_data_)); | 756 memset(&child_data_, 0, sizeof(child_data_)); |
682 child_data_.header = sparse_header_; | 757 child_data_.header = sparse_header_; |
683 | 758 |
684 scoped_refptr<net::WrappedIOBuffer> buf( | 759 scoped_refptr<net::WrappedIOBuffer> buf( |
685 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 760 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
686 | 761 |
687 int rv = child_->WriteData(kSparseIndex, | 762 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
688 0, | 763 CompletionCallback(), false); |
689 buf.get(), | |
690 sizeof(child_data_), | |
691 CompletionCallback(), | |
692 false); | |
693 if (rv != sizeof(child_data_)) | 764 if (rv != sizeof(child_data_)) |
694 DLOG(ERROR) << "Failed to save child data"; | 765 DLOG(ERROR) << "Failed to save child data"; |
695 SetChildBit(true); | 766 SetChildBit(true); |
696 } | 767 } |
697 | 768 |
698 void SparseControl::DoChildrenIO() { | |
699 while (DoChildIO()) continue; | |
700 | |
701 // Range operations are finished synchronously, often without setting | |
702 // |finished_| to true. | |
703 if (kGetRangeOperation == operation_ && | |
704 entry_->net_log().IsLoggingAllEvents()) { | |
705 entry_->net_log().EndEvent( | |
706 net::NetLog::TYPE_SPARSE_GET_RANGE, | |
707 CreateNetLogGetAvailableRangeResultCallback(offset_, result_)); | |
708 } | |
709 if (finished_) { | |
710 if (kGetRangeOperation != operation_ && | |
711 entry_->net_log().IsLoggingAllEvents()) { | |
712 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | |
713 } | |
714 if (pending_) | |
715 DoUserCallback(); // Don't touch this object after this point. | |
716 } | |
717 } | |
718 | |
719 bool SparseControl::DoChildIO() { | |
720 finished_ = true; | |
721 if (!buf_len_ || result_ < 0) | |
722 return false; | |
723 | |
724 if (!OpenChild()) | |
725 return false; | |
726 | |
727 if (!VerifyRange()) | |
728 return false; | |
729 | |
730 // We have more work to do. Let's not trigger a callback to the caller. | |
731 finished_ = false; | |
732 CompletionCallback callback; | |
733 if (!user_callback_.is_null()) { | |
734 callback = | |
735 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); | |
736 } | |
737 | |
738 int rv = 0; | |
739 switch (operation_) { | |
740 case kReadOperation: | |
741 if (entry_->net_log().IsLoggingAllEvents()) { | |
742 entry_->net_log().BeginEvent( | |
743 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | |
744 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
745 child_len_)); | |
746 } | |
747 rv = child_->ReadDataImpl( | |
748 kSparseData, child_offset_, user_buf_.get(), child_len_, callback); | |
749 break; | |
750 case kWriteOperation: | |
751 if (entry_->net_log().IsLoggingAllEvents()) { | |
752 entry_->net_log().BeginEvent( | |
753 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | |
754 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
755 child_len_)); | |
756 } | |
757 rv = child_->WriteDataImpl(kSparseData, | |
758 child_offset_, | |
759 user_buf_.get(), | |
760 child_len_, | |
761 callback, | |
762 false); | |
763 break; | |
764 case kGetRangeOperation: | |
765 rv = DoGetAvailableRange(); | |
766 break; | |
767 default: | |
768 NOTREACHED(); | |
769 } | |
770 | |
771 if (rv == net::ERR_IO_PENDING) { | |
772 if (!pending_) { | |
773 pending_ = true; | |
774 // The child will protect himself against closing the entry while IO is in | |
775 // progress. However, this entry can still be closed, and that would not | |
776 // be a good thing for us, so we increase the refcount until we're | |
777 // finished doing sparse stuff. | |
778 entry_->AddRef(); // Balanced in DoUserCallback. | |
779 } | |
780 return false; | |
781 } | |
782 if (!rv) | |
783 return false; | |
784 | |
785 DoChildIOCompleted(rv); | |
786 return true; | |
787 } | |
788 | |
789 int SparseControl::DoGetAvailableRange() { | 769 int SparseControl::DoGetAvailableRange() { |
790 if (!child_) | 770 if (!child_) |
791 return child_len_; // Move on to the next child. | 771 return child_len_; // Move on to the next child. |
792 | 772 |
793 // Check that there are no holes in this range. | 773 // Check that there are no holes in this range. |
794 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; | 774 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; |
795 int start = child_offset_ >> 10; | 775 int start = child_offset_ >> 10; |
796 int partial_start_bytes = PartialBlockLength(start); | 776 int partial_start_bytes = PartialBlockLength(start); |
797 int found = start; | 777 int found = start; |
798 int bits_found = child_map_.FindBits(&found, last_bit, true); | 778 int bits_found = child_map_.FindBits(&found, last_bit, true); |
(...skipping 27 matching lines...) Expand all Loading... |
826 | 806 |
827 // Only update offset_ when this query found zeros at the start. | 807 // Only update offset_ when this query found zeros at the start. |
828 if (empty_start) | 808 if (empty_start) |
829 offset_ += empty_start; | 809 offset_ += empty_start; |
830 | 810 |
831 // This will actually break the loop. | 811 // This will actually break the loop. |
832 buf_len_ = 0; | 812 buf_len_ = 0; |
833 return 0; | 813 return 0; |
834 } | 814 } |
835 | 815 |
836 void SparseControl::DoChildIOCompleted(int result) { | 816 void SparseControl::DoUserCallback() { |
837 LogChildOperationEnd(entry_->net_log(), operation_, result); | 817 DCHECK(!user_callback_.is_null()); |
838 if (result < 0) { | 818 CompletionCallback cb = user_callback_; |
839 // We fail the whole operation if we encounter an error. | 819 user_callback_.Reset(); |
840 result_ = result; | 820 user_buf_ = NULL; |
841 return; | 821 pending_ = false; |
| 822 operation_ = kNoOperation; |
| 823 int rv = result_; |
| 824 entry_->Release(); // Don't touch object after this line. |
| 825 cb.Run(rv); |
| 826 } |
| 827 |
| 828 void SparseControl::DoAbortCallbacks() { |
| 829 for (size_t i = 0; i < abort_callbacks_.size(); i++) { |
| 830 // Releasing all references to entry_ may result in the destruction of this |
| 831 // object so we should not be touching it after the last Release(). |
| 832 CompletionCallback cb = abort_callbacks_[i]; |
| 833 if (i == abort_callbacks_.size() - 1) |
| 834 abort_callbacks_.clear(); |
| 835 |
| 836 entry_->Release(); // Don't touch object after this line. |
| 837 cb.Run(net::OK); |
842 } | 838 } |
843 | |
844 UpdateRange(result); | |
845 | |
846 result_ += result; | |
847 offset_ += result; | |
848 buf_len_ -= result; | |
849 | |
850 // We'll be reusing the user provided buffer for the next chunk. | |
851 if (buf_len_ && user_buf_.get()) | |
852 user_buf_->DidConsume(result); | |
853 } | 839 } |
854 | 840 |
855 void SparseControl::OnChildIOCompleted(int result) { | 841 void SparseControl::OnChildIOCompleted(int result) { |
856 DCHECK_NE(net::ERR_IO_PENDING, result); | 842 DCHECK_NE(net::ERR_IO_PENDING, result); |
857 DoChildIOCompleted(result); | 843 DoChildIOCompleted(result); |
858 | 844 |
859 if (abort_) { | 845 if (abort_) { |
860 // We'll return the current result of the operation, which may be less than | 846 // We'll return the current result of the operation, which may be less than |
861 // the bytes to read or write, but the user cancelled the operation. | 847 // the bytes to read or write, but the user cancelled the operation. |
862 abort_ = false; | 848 abort_ = false; |
863 if (entry_->net_log().IsLoggingAllEvents()) { | 849 if (entry_->net_log().IsLoggingAllEvents()) { |
864 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); | 850 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); |
865 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | 851 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
866 } | 852 } |
867 // We have an indirect reference to this object for every callback so if | 853 // We have an indirect reference to this object for every callback so if |
868 // there is only one callback, we may delete this object before reaching | 854 // there is only one callback, we may delete this object before reaching |
869 // DoAbortCallbacks. | 855 // DoAbortCallbacks. |
870 bool has_abort_callbacks = !abort_callbacks_.empty(); | 856 bool has_abort_callbacks = !abort_callbacks_.empty(); |
871 DoUserCallback(); | 857 DoUserCallback(); |
872 if (has_abort_callbacks) | 858 if (has_abort_callbacks) |
873 DoAbortCallbacks(); | 859 DoAbortCallbacks(); |
874 return; | 860 return; |
875 } | 861 } |
876 | 862 |
877 // We are running a callback from the message loop. It's time to restart what | 863 // We are running a callback from the message loop. It's time to restart what |
878 // we were doing before. | 864 // we were doing before. |
879 DoChildrenIO(); | 865 DoChildrenIO(); |
880 } | 866 } |
881 | 867 |
882 void SparseControl::DoUserCallback() { | |
883 DCHECK(!user_callback_.is_null()); | |
884 CompletionCallback cb = user_callback_; | |
885 user_callback_.Reset(); | |
886 user_buf_ = NULL; | |
887 pending_ = false; | |
888 operation_ = kNoOperation; | |
889 int rv = result_; | |
890 entry_->Release(); // Don't touch object after this line. | |
891 cb.Run(rv); | |
892 } | |
893 | |
894 void SparseControl::DoAbortCallbacks() { | |
895 for (size_t i = 0; i < abort_callbacks_.size(); i++) { | |
896 // Releasing all references to entry_ may result in the destruction of this | |
897 // object so we should not be touching it after the last Release(). | |
898 CompletionCallback cb = abort_callbacks_[i]; | |
899 if (i == abort_callbacks_.size() - 1) | |
900 abort_callbacks_.clear(); | |
901 | |
902 entry_->Release(); // Don't touch object after this line. | |
903 cb.Run(net::OK); | |
904 } | |
905 } | |
906 | |
907 } // namespace disk_cache | 868 } // namespace disk_cache |
OLD | NEW |