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