1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "net/disk_cache/blockfile/block_files.h"
11
12 #include <atomic>
13 #include <limits>
14 #include <memory>
15 #include <optional>
16
17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/threading/thread_checker.h"
22 #include "base/time/time.h"
23 #include "net/disk_cache/blockfile/file_lock.h"
24 #include "net/disk_cache/blockfile/stress_support.h"
25 #include "net/disk_cache/cache_util.h"
26
27 using base::TimeTicks;
28
29 namespace {
30
31 const char kBlockName[] = "data_";
32
33 // This array is used to perform a fast lookup of the nibble bit pattern to the
34 // type of entry that can be stored there (number of consecutive blocks).
35 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
36
37 // Returns the type of block (number of consecutive blocks that can be stored)
38 // for a given nibble of the bitmap.
GetMapBlockType(uint32_t value)39 inline int GetMapBlockType(uint32_t value) {
40 value &= 0xf;
41 return s_types[value];
42 }
43
44 } // namespace
45
46 namespace disk_cache {
47
BlockHeader()48 BlockHeader::BlockHeader() : header_(nullptr) {}
49
BlockHeader(BlockFileHeader * header)50 BlockHeader::BlockHeader(BlockFileHeader* header) : header_(header) {
51 }
52
BlockHeader(MappedFile * file)53 BlockHeader::BlockHeader(MappedFile* file)
54 : header_(reinterpret_cast<BlockFileHeader*>(file->buffer())) {
55 }
56
57 BlockHeader::BlockHeader(const BlockHeader& other) = default;
58
59 BlockHeader::~BlockHeader() = default;
60
CreateMapBlock(int size,int * index)61 bool BlockHeader::CreateMapBlock(int size, int* index) {
62 DCHECK(size > 0 && size <= kMaxNumBlocks);
63 int target = 0;
64 for (int i = size; i <= kMaxNumBlocks; i++) {
65 if (header_->empty[i - 1]) {
66 target = i;
67 break;
68 }
69 }
70
71 if (!target) {
72 STRESS_NOTREACHED();
73 return false;
74 }
75
76 // We are going to process the map on 32-block chunks (32 bits), and on every
77 // chunk, iterate through the 8 nibbles where the new block can be located.
78 int current = header_->hints[target - 1];
79 for (int i = 0; i < header_->max_entries / 32; i++, current++) {
80 if (current == header_->max_entries / 32)
81 current = 0;
82 uint32_t map_block = header_->allocation_map[current];
83
84 for (int j = 0; j < 8; j++, map_block >>= 4) {
85 if (GetMapBlockType(map_block) != target)
86 continue;
87
88 disk_cache::FileLock lock(header_);
89 int index_offset = j * 4 + 4 - target;
90 *index = current * 32 + index_offset;
91 STRESS_DCHECK(*index / 4 == (*index + size - 1) / 4);
92 uint32_t to_add = ((1 << size) - 1) << index_offset;
93 header_->num_entries++;
94
95 // Note that there is no race in the normal sense here, but if we enforce
96 // the order of memory accesses between num_entries and allocation_map, we
97 // can assert that even if we crash here, num_entries will never be less
98 // than the actual number of used blocks.
99 std::atomic_thread_fence(std::memory_order_seq_cst);
100 header_->allocation_map[current] |= to_add;
101
102 header_->hints[target - 1] = current;
103 header_->empty[target - 1]--;
104 STRESS_DCHECK(header_->empty[target - 1] >= 0);
105 if (target != size) {
106 header_->empty[target - size - 1]++;
107 }
108 return true;
109 }
110 }
111
112 // It is possible to have an undetected corruption (for example when the OS
113 // crashes), fix it here.
114 LOG(ERROR) << "Failing CreateMapBlock";
115 FixAllocationCounters();
116 return false;
117 }
118
DeleteMapBlock(int index,int size)119 void BlockHeader::DeleteMapBlock(int index, int size) {
120 if (size < 0 || size > kMaxNumBlocks) {
121 NOTREACHED();
122 }
123 int byte_index = index / 8;
124 uint8_t* byte_map = reinterpret_cast<uint8_t*>(header_->allocation_map);
125 uint8_t map_block = byte_map[byte_index];
126
127 if (index % 8 >= 4)
128 map_block >>= 4;
129
130 // See what type of block will be available after we delete this one.
131 int bits_at_end = 4 - size - index % 4;
132 uint8_t end_mask = (0xf << (4 - bits_at_end)) & 0xf;
133 bool update_counters = (map_block & end_mask) == 0;
134 uint8_t new_value = map_block & ~(((1 << size) - 1) << (index % 4));
135 int new_type = GetMapBlockType(new_value);
136
137 disk_cache::FileLock lock(header_);
138 STRESS_DCHECK((((1 << size) - 1) << (index % 8)) < 0x100);
139 uint8_t to_clear = ((1 << size) - 1) << (index % 8);
140 STRESS_DCHECK((byte_map[byte_index] & to_clear) == to_clear);
141 byte_map[byte_index] &= ~to_clear;
142
143 if (update_counters) {
144 if (bits_at_end)
145 header_->empty[bits_at_end - 1]--;
146 header_->empty[new_type - 1]++;
147 STRESS_DCHECK(header_->empty[bits_at_end - 1] >= 0);
148 }
149 std::atomic_thread_fence(std::memory_order_seq_cst);
150 header_->num_entries--;
151 STRESS_DCHECK(header_->num_entries >= 0);
152 }
153
154 // Note that this is a simplified version of DeleteMapBlock().
UsedMapBlock(int index,int size)155 bool BlockHeader::UsedMapBlock(int index, int size) {
156 if (size < 0 || size > kMaxNumBlocks)
157 return false;
158
159 int byte_index = index / 8;
160 uint8_t* byte_map = reinterpret_cast<uint8_t*>(header_->allocation_map);
161
162 STRESS_DCHECK((((1 << size) - 1) << (index % 8)) < 0x100);
163 uint8_t to_clear = ((1 << size) - 1) << (index % 8);
164 return ((byte_map[byte_index] & to_clear) == to_clear);
165 }
166
FixAllocationCounters()167 void BlockHeader::FixAllocationCounters() {
168 for (int i = 0; i < kMaxNumBlocks; i++) {
169 header_->hints[i] = 0;
170 header_->empty[i] = 0;
171 }
172
173 for (int i = 0; i < header_->max_entries / 32; i++) {
174 uint32_t map_block = header_->allocation_map[i];
175
176 for (int j = 0; j < 8; j++, map_block >>= 4) {
177 int type = GetMapBlockType(map_block);
178 if (type)
179 header_->empty[type -1]++;
180 }
181 }
182 }
183
NeedToGrowBlockFile(int block_count) const184 bool BlockHeader::NeedToGrowBlockFile(int block_count) const {
185 bool have_space = false;
186 int empty_blocks = 0;
187 for (int i = 0; i < kMaxNumBlocks; i++) {
188 empty_blocks += header_->empty[i] * (i + 1);
189 if (i >= block_count - 1 && header_->empty[i])
190 have_space = true;
191 }
192
193 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) {
194 // This file is almost full but we already created another one, don't use
195 // this file yet so that it is easier to find empty blocks when we start
196 // using this file again.
197 return true;
198 }
199 return !have_space;
200 }
201
CanAllocate(int block_count) const202 bool BlockHeader::CanAllocate(int block_count) const {
203 DCHECK_GT(block_count, 0);
204 for (int i = block_count - 1; i < kMaxNumBlocks; i++) {
205 if (header_->empty[i])
206 return true;
207 }
208
209 return false;
210 }
211
EmptyBlocks() const212 int BlockHeader::EmptyBlocks() const {
213 int empty_blocks = 0;
214 for (int i = 0; i < kMaxNumBlocks; i++) {
215 empty_blocks += header_->empty[i] * (i + 1);
216 if (header_->empty[i] < 0)
217 return 0;
218 }
219 return empty_blocks;
220 }
221
MinimumAllocations() const222 int BlockHeader::MinimumAllocations() const {
223 return header_->empty[kMaxNumBlocks - 1];
224 }
225
Capacity() const226 int BlockHeader::Capacity() const {
227 return header_->max_entries;
228 }
229
ValidateCounters() const230 bool BlockHeader::ValidateCounters() const {
231 if (header_->max_entries < 0 || header_->max_entries > kMaxBlocks ||
232 header_->num_entries < 0)
233 return false;
234
235 int empty_blocks = EmptyBlocks();
236 if (empty_blocks + header_->num_entries > header_->max_entries)
237 return false;
238
239 return true;
240 }
241
FileId() const242 int BlockHeader::FileId() const {
243 return header_->this_file;
244 }
245
NextFileId() const246 int BlockHeader::NextFileId() const {
247 return header_->next_file;
248 }
249
Size() const250 int BlockHeader::Size() const {
251 return static_cast<int>(sizeof(*header_));
252 }
253
Header()254 BlockFileHeader* BlockHeader::Header() {
255 return header_;
256 }
257
258 // ------------------------------------------------------------------------
259
BlockFiles(const base::FilePath & path)260 BlockFiles::BlockFiles(const base::FilePath& path) : path_(path) {}
261
~BlockFiles()262 BlockFiles::~BlockFiles() {
263 CloseFiles();
264 }
265
Init(bool create_files)266 bool BlockFiles::Init(bool create_files) {
267 DCHECK(!init_);
268 if (init_)
269 return false;
270
271 thread_checker_ = std::make_unique<base::ThreadChecker>();
272
273 block_files_.resize(kFirstAdditionalBlockFile);
274 for (int16_t i = 0; i < kFirstAdditionalBlockFile; i++) {
275 if (create_files)
276 if (!CreateBlockFile(i, static_cast<FileType>(i + 1), true))
277 return false;
278
279 if (!OpenBlockFile(i))
280 return false;
281
282 // Walk this chain of files removing empty ones.
283 if (!RemoveEmptyFile(static_cast<FileType>(i + 1)))
284 return false;
285 }
286
287 init_ = true;
288 return true;
289 }
290
GetFile(Addr address)291 MappedFile* BlockFiles::GetFile(Addr address) {
292 DCHECK(thread_checker_->CalledOnValidThread());
293 DCHECK_GE(block_files_.size(),
294 static_cast<size_t>(kFirstAdditionalBlockFile));
295 DCHECK(address.is_block_file() || !address.is_initialized());
296 if (!address.is_initialized())
297 return nullptr;
298
299 int file_index = address.FileNumber();
300 if (static_cast<unsigned int>(file_index) >= block_files_.size() ||
301 !block_files_[file_index]) {
302 // We need to open the file
303 if (!OpenBlockFile(file_index))
304 return nullptr;
305 }
306 DCHECK_GE(block_files_.size(), static_cast<unsigned int>(file_index));
307 return block_files_[file_index].get();
308 }
309
CreateBlock(FileType block_type,int block_count,Addr * block_address)310 bool BlockFiles::CreateBlock(FileType block_type, int block_count,
311 Addr* block_address) {
312 DCHECK(thread_checker_->CalledOnValidThread());
313 DCHECK_NE(block_type, EXTERNAL);
314 DCHECK_NE(block_type, BLOCK_FILES);
315 DCHECK_NE(block_type, BLOCK_ENTRIES);
316 DCHECK_NE(block_type, BLOCK_EVICTED);
317 if (block_count < 1 || block_count > kMaxNumBlocks)
318 return false;
319
320 if (!init_)
321 return false;
322
323 MappedFile* file = FileForNewBlock(block_type, block_count);
324 if (!file)
325 return false;
326
327 ScopedFlush flush(file);
328 BlockHeader file_header(file);
329
330 int index;
331 if (!file_header.CreateMapBlock(block_count, &index))
332 return false;
333
334 Addr address(block_type, block_count, file_header.FileId(), index);
335 block_address->set_value(address.value());
336 return true;
337 }
338
DeleteBlock(Addr address,bool deep)339 void BlockFiles::DeleteBlock(Addr address, bool deep) {
340 DCHECK(thread_checker_->CalledOnValidThread());
341 if (!address.is_initialized() || address.is_separate_file())
342 return;
343
344 MappedFile* file = GetFile(address);
345 if (!file)
346 return;
347
348 if (zero_buffer_.empty())
349 zero_buffer_.resize(Addr::BlockSizeForFileType(BLOCK_4K) * 4, 0);
350
351 size_t size = address.BlockSize() * address.num_blocks();
352 size_t offset = address.start_block() * address.BlockSize() +
353 kBlockHeaderSize;
354 if (deep)
355 file->Write(zero_buffer_.data(), size, offset);
356
357 std::optional<FileType> type_to_delete;
358 {
359 // Block Header can't outlive file's buffer.
360 BlockHeader file_header(file);
361 file_header.DeleteMapBlock(address.start_block(), address.num_blocks());
362 file->Flush();
363
364 if (!file_header.Header()->num_entries) {
365 // This file is now empty. Let's try to delete it.
366 type_to_delete = Addr::RequiredFileType(file_header.Header()->entry_size);
367 if (Addr::BlockSizeForFileType(RANKINGS) ==
368 file_header.Header()->entry_size) {
369 type_to_delete = RANKINGS;
370 }
371 }
372 }
373 if (type_to_delete.has_value()) {
374 RemoveEmptyFile(type_to_delete.value()); // Ignore failures.
375 }
376 }
377
CloseFiles()378 void BlockFiles::CloseFiles() {
379 if (init_) {
380 DCHECK(thread_checker_->CalledOnValidThread());
381 }
382 init_ = false;
383 block_files_.clear();
384 }
385
IsValid(Addr address)386 bool BlockFiles::IsValid(Addr address) {
387 #ifdef NDEBUG
388 return true;
389 #else
390 if (!address.is_initialized() || address.is_separate_file())
391 return false;
392
393 MappedFile* file = GetFile(address);
394 if (!file)
395 return false;
396
397 BlockHeader header(file);
398 bool rv = header.UsedMapBlock(address.start_block(), address.num_blocks());
399 DCHECK(rv);
400
401 static bool read_contents = false;
402 if (read_contents) {
403 auto buffer =
404 std::make_unique<char[]>(Addr::BlockSizeForFileType(BLOCK_4K) * 4);
405 size_t size = address.BlockSize() * address.num_blocks();
406 size_t offset = address.start_block() * address.BlockSize() +
407 kBlockHeaderSize;
408 bool ok = file->Read(buffer.get(), size, offset);
409 DCHECK(ok);
410 }
411
412 return rv;
413 #endif
414 }
415
CreateBlockFile(int index,FileType file_type,bool force)416 bool BlockFiles::CreateBlockFile(int index, FileType file_type, bool force) {
417 base::FilePath name = Name(index);
418 int flags = force ? base::File::FLAG_CREATE_ALWAYS : base::File::FLAG_CREATE;
419 flags |= base::File::FLAG_WRITE | base::File::FLAG_WIN_EXCLUSIVE_WRITE;
420
421 auto file = base::MakeRefCounted<File>(base::File(name, flags));
422 if (!file->IsValid())
423 return false;
424
425 BlockFileHeader header;
426 memset(&header, 0, sizeof(header));
427 header.magic = kBlockMagic;
428 header.version = kBlockVersion2;
429 header.entry_size = Addr::BlockSizeForFileType(file_type);
430 header.this_file = static_cast<int16_t>(index);
431 DCHECK(index <= std::numeric_limits<int16_t>::max() && index >= 0);
432
433 return file->Write(&header, sizeof(header), 0);
434 }
435
OpenBlockFile(int index)436 bool BlockFiles::OpenBlockFile(int index) {
437 if (block_files_.size() - 1 < static_cast<unsigned int>(index)) {
438 DCHECK(index > 0);
439 int to_add = index - static_cast<int>(block_files_.size()) + 1;
440 block_files_.resize(block_files_.size() + to_add);
441 }
442
443 base::FilePath name = Name(index);
444 auto file = base::MakeRefCounted<MappedFile>();
445
446 if (!file->Init(name, kBlockHeaderSize)) {
447 LOG(ERROR) << "Failed to open " << name.value();
448 return false;
449 }
450
451 size_t file_len = file->GetLength();
452 if (file_len < static_cast<size_t>(kBlockHeaderSize)) {
453 LOG(ERROR) << "File too small " << name.value();
454 return false;
455 }
456
457 BlockHeader file_header(file.get());
458 BlockFileHeader* header = file_header.Header();
459 if (kBlockMagic != header->magic || kBlockVersion2 != header->version) {
460 LOG(ERROR) << "Invalid file version or magic " << name.value();
461 return false;
462 }
463
464 if (header->updating || !file_header.ValidateCounters()) {
465 // Last instance was not properly shutdown, or counters are out of sync.
466 if (!FixBlockFileHeader(file.get())) {
467 LOG(ERROR) << "Unable to fix block file " << name.value();
468 return false;
469 }
470 }
471
472 if (static_cast<int>(file_len) <
473 header->max_entries * header->entry_size + kBlockHeaderSize) {
474 LOG(ERROR) << "File too small " << name.value();
475 return false;
476 }
477
478 if (index == 0) {
479 // Load the links file into memory.
480 if (!file->Preload())
481 return false;
482 }
483
484 ScopedFlush flush(file.get());
485 DCHECK(!block_files_[index]);
486 block_files_[index] = std::move(file);
487 return true;
488 }
489
GrowBlockFile(MappedFile * file,BlockFileHeader * header)490 bool BlockFiles::GrowBlockFile(MappedFile* file, BlockFileHeader* header) {
491 if (kMaxBlocks == header->max_entries)
492 return false;
493
494 ScopedFlush flush(file);
495 DCHECK(!header->empty[3]);
496 int new_size = header->max_entries + 1024;
497 if (new_size > kMaxBlocks)
498 new_size = kMaxBlocks;
499
500 int new_size_bytes = new_size * header->entry_size + sizeof(*header);
501
502 if (!file->SetLength(new_size_bytes)) {
503 // Most likely we are trying to truncate the file, so the header is wrong.
504 if (header->updating < 10 && !FixBlockFileHeader(file)) {
505 // If we can't fix the file increase the lock guard so we'll pick it on
506 // the next start and replace it.
507 header->updating = 100;
508 return false;
509 }
510 return (header->max_entries >= new_size);
511 }
512
513 FileLock lock(header);
514 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries
515 header->max_entries = new_size;
516
517 return true;
518 }
519
FileForNewBlock(FileType block_type,int block_count)520 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) {
521 static_assert(RANKINGS == 1, "invalid file type");
522 MappedFile* file = block_files_[block_type - 1].get();
523 BlockHeader file_header(file);
524
525 while (file_header.NeedToGrowBlockFile(block_count)) {
526 if (kMaxBlocks == file_header.Header()->max_entries) {
527 file = NextFile(file);
528 if (!file)
529 return nullptr;
530 file_header = BlockHeader(file);
531 continue;
532 }
533
534 if (!GrowBlockFile(file, file_header.Header()))
535 return nullptr;
536 break;
537 }
538 return file;
539 }
540
NextFile(MappedFile * file)541 MappedFile* BlockFiles::NextFile(MappedFile* file) {
542 ScopedFlush flush(file);
543 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
544 int16_t new_file = header->next_file;
545 if (!new_file) {
546 // RANKINGS is not reported as a type for small entries, but we may be
547 // extending the rankings block file.
548 FileType type = Addr::RequiredFileType(header->entry_size);
549 if (header->entry_size == Addr::BlockSizeForFileType(RANKINGS))
550 type = RANKINGS;
551
552 new_file = CreateNextBlockFile(type);
553 if (!new_file)
554 return nullptr;
555
556 FileLock lock(header);
557 header->next_file = new_file;
558 }
559
560 // Only the block_file argument is relevant for what we want.
561 Addr address(BLOCK_256, 1, new_file, 0);
562 return GetFile(address);
563 }
564
CreateNextBlockFile(FileType block_type)565 int16_t BlockFiles::CreateNextBlockFile(FileType block_type) {
566 for (int16_t i = kFirstAdditionalBlockFile; i <= kMaxBlockFile; i++) {
567 if (CreateBlockFile(i, block_type, false))
568 return i;
569 }
570 return 0;
571 }
572
573 // We walk the list of files for this particular block type, deleting the ones
574 // that are empty.
RemoveEmptyFile(FileType block_type)575 bool BlockFiles::RemoveEmptyFile(FileType block_type) {
576 MappedFile* file = block_files_[block_type - 1].get();
577 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
578
579 while (header->next_file) {
580 // Only the block_file argument is relevant for what we want.
581 Addr address(BLOCK_256, 1, header->next_file, 0);
582 MappedFile* next_file = GetFile(address);
583 if (!next_file)
584 return false;
585
586 BlockFileHeader* next_header =
587 reinterpret_cast<BlockFileHeader*>(next_file->buffer());
588 if (!next_header->num_entries) {
589 DCHECK_EQ(next_header->entry_size, header->entry_size);
590 // Delete next_file and remove it from the chain.
591 int file_index = header->next_file;
592 header->next_file = next_header->next_file;
593 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index));
594 file->Flush();
595
596 // We get a new handle to the file and release the old one so that the
597 // file gets unmmaped... so we can delete it.
598 base::FilePath name = Name(file_index);
599 auto this_file = base::MakeRefCounted<File>(false);
600 this_file->Init(name);
601 block_files_[file_index] = nullptr;
602
603 int failure = base::DeleteFile(name) ? 0 : 1;
604 if (failure)
605 LOG(ERROR) << "Failed to delete " << name.value() << " from the cache.";
606 continue;
607 }
608
609 header = next_header;
610 file = next_file;
611 }
612 return true;
613 }
614
615 // Note that we expect to be called outside of a FileLock... however, we cannot
616 // DCHECK on header->updating because we may be fixing a crash.
FixBlockFileHeader(MappedFile * file)617 bool BlockFiles::FixBlockFileHeader(MappedFile* file) {
618 ScopedFlush flush(file);
619 BlockHeader file_header(file);
620 int file_size = static_cast<int>(file->GetLength());
621 if (file_size < file_header.Size())
622 return false; // file_size > 2GB is also an error.
623
624 const int kMinHeaderBlockSize = 36;
625 const int kMaxHeaderBlockSize = 4096;
626 BlockFileHeader* header = file_header.Header();
627 if (header->entry_size < kMinHeaderBlockSize ||
628 header->entry_size > kMaxHeaderBlockSize || header->num_entries < 0)
629 return false;
630
631 // Make sure that we survive crashes.
632 header->updating = 1;
633 int expected = header->entry_size * header->max_entries + file_header.Size();
634 if (file_size != expected) {
635 int max_expected = header->entry_size * kMaxBlocks + file_header.Size();
636 if (file_size < expected || header->empty[3] || file_size > max_expected) {
637 LOG(ERROR) << "Unexpected file size";
638 return false;
639 }
640 // We were in the middle of growing the file.
641 int num_entries = (file_size - file_header.Size()) / header->entry_size;
642 header->max_entries = num_entries;
643 }
644
645 file_header.FixAllocationCounters();
646 int empty_blocks = file_header.EmptyBlocks();
647 if (empty_blocks + header->num_entries > header->max_entries)
648 header->num_entries = header->max_entries - empty_blocks;
649
650 if (!file_header.ValidateCounters())
651 return false;
652
653 header->updating = 0;
654 return true;
655 }
656
Name(int index)657 base::FilePath BlockFiles::Name(int index) {
658 // The file format allows for 256 files.
659 DCHECK(index < 256 && index >= 0);
660 std::string tmp = base::StringPrintf("%s%d", kBlockName, index);
661 return path_.AppendASCII(tmp);
662 }
663
664 } // namespace disk_cache
665