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