• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/rankings.h"
6 
7 #include <stdint.h>
8 
9 #include <limits>
10 #include <memory>
11 
12 #include "base/memory/raw_ptr.h"
13 #include "base/process/process.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "net/base/net_export.h"
17 #include "net/disk_cache/blockfile/backend_impl.h"
18 #include "net/disk_cache/blockfile/disk_format.h"
19 #include "net/disk_cache/blockfile/entry_impl.h"
20 #include "net/disk_cache/blockfile/errors.h"
21 #include "net/disk_cache/blockfile/histogram_macros.h"
22 #include "net/disk_cache/blockfile/stress_support.h"
23 
24 #if BUILDFLAG(IS_WIN)
25 #include <windows.h>
26 #endif
27 
28 // Provide a BackendImpl object to macros from histogram_macros.h.
29 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_
30 
31 using base::Time;
32 using base::TimeTicks;
33 
34 namespace disk_cache {
35 // This is used by crash_cache.exe to generate unit test files.
36 NET_EXPORT_PRIVATE RankCrashes g_rankings_crash = NO_CRASH;
37 }
38 
39 namespace {
40 
41 enum Operation {
42   INSERT = 1,
43   REMOVE
44 };
45 
46 // This class provides a simple lock for the LRU list of rankings. Whenever an
47 // entry is to be inserted or removed from the list, a transaction object should
48 // be created to keep track of the operation. If the process crashes before
49 // finishing the operation, the transaction record (stored as part of the user
50 // data on the file header) can be used to finish the operation.
51 class Transaction {
52  public:
53   // addr is the cache address of the node being inserted or removed. We want to
54   // avoid having the compiler doing optimizations on when to read or write
55   // from user_data because it is the basis of the crash detection. Maybe
56   // volatile is not enough for that, but it should be a good hint.
57   Transaction(volatile disk_cache::LruData* data, disk_cache::Addr addr,
58               Operation op, int list);
59 
60   Transaction(const Transaction&) = delete;
61   Transaction& operator=(const Transaction&) = delete;
62 
63   ~Transaction();
64  private:
65   raw_ptr<volatile disk_cache::LruData> data_;
66 };
67 
Transaction(volatile disk_cache::LruData * data,disk_cache::Addr addr,Operation op,int list)68 Transaction::Transaction(volatile disk_cache::LruData* data,
69                          disk_cache::Addr addr, Operation op, int list)
70     : data_(data) {
71   DCHECK(!data_->transaction);
72   DCHECK(addr.is_initialized());
73   data_->operation = op;
74   data_->operation_list = list;
75   data_->transaction = addr.value();
76 }
77 
~Transaction()78 Transaction::~Transaction() {
79   DCHECK(data_->transaction);
80   data_->transaction = 0;
81   data_->operation = 0;
82   data_->operation_list = 0;
83 }
84 
85 // Code locations that can generate crashes.
86 enum CrashLocation {
87   ON_INSERT_1, ON_INSERT_2, ON_INSERT_3, ON_INSERT_4, ON_REMOVE_1, ON_REMOVE_2,
88   ON_REMOVE_3, ON_REMOVE_4, ON_REMOVE_5, ON_REMOVE_6, ON_REMOVE_7, ON_REMOVE_8
89 };
90 
91 // Simulates a crash (by exiting the process without graceful shutdown) on debug
92 // builds, according to the value of g_rankings_crash. This used by
93 // crash_cache.exe to generate unit-test files.
GenerateCrash(CrashLocation location)94 void GenerateCrash(CrashLocation location) {
95 #if !defined(NDEBUG) && !BUILDFLAG(IS_IOS)
96   if (disk_cache::NO_CRASH == disk_cache::g_rankings_crash)
97     return;
98   switch (location) {
99     case ON_INSERT_1:
100       switch (disk_cache::g_rankings_crash) {
101         case disk_cache::INSERT_ONE_1:
102         case disk_cache::INSERT_LOAD_1:
103           base::Process::TerminateCurrentProcessImmediately(0);
104         default:
105           break;
106       }
107       break;
108     case ON_INSERT_2:
109       if (disk_cache::INSERT_EMPTY_1 == disk_cache::g_rankings_crash)
110         base::Process::TerminateCurrentProcessImmediately(0);
111       break;
112     case ON_INSERT_3:
113       switch (disk_cache::g_rankings_crash) {
114         case disk_cache::INSERT_EMPTY_2:
115         case disk_cache::INSERT_ONE_2:
116         case disk_cache::INSERT_LOAD_2:
117           base::Process::TerminateCurrentProcessImmediately(0);
118         default:
119           break;
120       }
121       break;
122     case ON_INSERT_4:
123       switch (disk_cache::g_rankings_crash) {
124         case disk_cache::INSERT_EMPTY_3:
125         case disk_cache::INSERT_ONE_3:
126           base::Process::TerminateCurrentProcessImmediately(0);
127         default:
128           break;
129       }
130       break;
131     case ON_REMOVE_1:
132       switch (disk_cache::g_rankings_crash) {
133         case disk_cache::REMOVE_ONE_1:
134         case disk_cache::REMOVE_HEAD_1:
135         case disk_cache::REMOVE_TAIL_1:
136         case disk_cache::REMOVE_LOAD_1:
137           base::Process::TerminateCurrentProcessImmediately(0);
138         default:
139           break;
140       }
141       break;
142     case ON_REMOVE_2:
143       if (disk_cache::REMOVE_ONE_2 == disk_cache::g_rankings_crash)
144         base::Process::TerminateCurrentProcessImmediately(0);
145       break;
146     case ON_REMOVE_3:
147       if (disk_cache::REMOVE_ONE_3 == disk_cache::g_rankings_crash)
148         base::Process::TerminateCurrentProcessImmediately(0);
149       break;
150     case ON_REMOVE_4:
151       if (disk_cache::REMOVE_HEAD_2 == disk_cache::g_rankings_crash)
152         base::Process::TerminateCurrentProcessImmediately(0);
153       break;
154     case ON_REMOVE_5:
155       if (disk_cache::REMOVE_TAIL_2 == disk_cache::g_rankings_crash)
156         base::Process::TerminateCurrentProcessImmediately(0);
157       break;
158     case ON_REMOVE_6:
159       if (disk_cache::REMOVE_TAIL_3 == disk_cache::g_rankings_crash)
160         base::Process::TerminateCurrentProcessImmediately(0);
161       break;
162     case ON_REMOVE_7:
163       switch (disk_cache::g_rankings_crash) {
164         case disk_cache::REMOVE_ONE_4:
165         case disk_cache::REMOVE_LOAD_2:
166         case disk_cache::REMOVE_HEAD_3:
167           base::Process::TerminateCurrentProcessImmediately(0);
168         default:
169           break;
170       }
171       break;
172     case ON_REMOVE_8:
173       switch (disk_cache::g_rankings_crash) {
174         case disk_cache::REMOVE_HEAD_4:
175         case disk_cache::REMOVE_LOAD_3:
176           base::Process::TerminateCurrentProcessImmediately(0);
177         default:
178           break;
179       }
180       break;
181     default:
182       NOTREACHED();
183       return;
184   }
185 #endif  // NDEBUG
186 }
187 
188 // Update the timestamp fields of |node|.
UpdateTimes(disk_cache::CacheRankingsBlock * node,bool modified)189 void UpdateTimes(disk_cache::CacheRankingsBlock* node, bool modified) {
190   base::Time now = base::Time::Now();
191   node->Data()->last_used = now.ToInternalValue();
192   if (modified)
193     node->Data()->last_modified = now.ToInternalValue();
194 }
195 
196 }  // namespace
197 
198 namespace disk_cache {
199 
ScopedRankingsBlock()200 Rankings::ScopedRankingsBlock::ScopedRankingsBlock() : rankings_(nullptr) {}
201 
ScopedRankingsBlock(Rankings * rankings)202 Rankings::ScopedRankingsBlock::ScopedRankingsBlock(Rankings* rankings)
203     : rankings_(rankings) {}
204 
ScopedRankingsBlock(Rankings * rankings,CacheRankingsBlock * node)205 Rankings::ScopedRankingsBlock::ScopedRankingsBlock(Rankings* rankings,
206                                                    CacheRankingsBlock* node)
207     : std::unique_ptr<CacheRankingsBlock>(node), rankings_(rankings) {}
208 
209 Rankings::Iterator::Iterator() = default;
210 
Reset()211 void Rankings::Iterator::Reset() {
212   if (my_rankings) {
213     for (auto* node : nodes) {
214       ScopedRankingsBlock(my_rankings, node);
215     }
216   }
217   my_rankings = nullptr;
218   nodes = {nullptr, nullptr, nullptr};
219   list = List::NO_USE;
220 }
221 
222 Rankings::Rankings() = default;
223 
224 Rankings::~Rankings() = default;
225 
Init(BackendImpl * backend,bool count_lists)226 bool Rankings::Init(BackendImpl* backend, bool count_lists) {
227   DCHECK(!init_);
228   if (init_)
229     return false;
230 
231   backend_ = backend;
232   control_data_ = backend_->GetLruData();
233   count_lists_ = count_lists;
234 
235   ReadHeads();
236   ReadTails();
237 
238   if (control_data_->transaction)
239     CompleteTransaction();
240 
241   init_ = true;
242   return true;
243 }
244 
Reset()245 void Rankings::Reset() {
246   init_ = false;
247   for (int i = 0; i < LAST_ELEMENT; i++) {
248     heads_[i].set_value(0);
249     tails_[i].set_value(0);
250   }
251   control_data_ = nullptr;
252 }
253 
Insert(CacheRankingsBlock * node,bool modified,List list)254 void Rankings::Insert(CacheRankingsBlock* node, bool modified, List list) {
255   DCHECK(node->HasData());
256   Addr& my_head = heads_[list];
257   Addr& my_tail = tails_[list];
258   Transaction lock(control_data_, node->address(), INSERT, list);
259   CacheRankingsBlock head(backend_->File(my_head), my_head);
260   if (my_head.is_initialized()) {
261     if (!GetRanking(&head))
262       return;
263 
264     if (head.Data()->prev != my_head.value() &&  // Normal path.
265         head.Data()->prev != node->address().value()) {  // FinishInsert().
266       backend_->CriticalError(ERR_INVALID_LINKS);
267       return;
268     }
269 
270     head.Data()->prev = node->address().value();
271     head.Store();
272     GenerateCrash(ON_INSERT_1);
273     UpdateIterators(&head);
274   }
275 
276   node->Data()->next = my_head.value();
277   node->Data()->prev = node->address().value();
278   my_head.set_value(node->address().value());
279 
280   if (!my_tail.is_initialized() || my_tail.value() == node->address().value()) {
281     my_tail.set_value(node->address().value());
282     node->Data()->next = my_tail.value();
283     WriteTail(list);
284     GenerateCrash(ON_INSERT_2);
285   }
286 
287   UpdateTimes(node, modified);
288   node->Store();
289   // Make sure other aliased in-memory copies get synchronized.
290   UpdateIterators(node);
291   GenerateCrash(ON_INSERT_3);
292 
293   // The last thing to do is move our head to point to a node already stored.
294   WriteHead(list);
295   IncrementCounter(list);
296   GenerateCrash(ON_INSERT_4);
297   backend_->FlushIndex();
298 }
299 
300 // If a, b and r are elements on the list, and we want to remove r, the possible
301 // states for the objects if a crash happens are (where y(x, z) means for object
302 // y, prev is x and next is z):
303 // A. One element:
304 //    1. r(r, r), head(r), tail(r)                    initial state
305 //    2. r(r, r), head(0), tail(r)                    WriteHead()
306 //    3. r(r, r), head(0), tail(0)                    WriteTail()
307 //    4. r(0, 0), head(0), tail(0)                    next.Store()
308 //
309 // B. Remove a random element:
310 //    1. a(x, r), r(a, b), b(r, y), head(x), tail(y)  initial state
311 //    2. a(x, r), r(a, b), b(a, y), head(x), tail(y)  next.Store()
312 //    3. a(x, b), r(a, b), b(a, y), head(x), tail(y)  prev.Store()
313 //    4. a(x, b), r(0, 0), b(a, y), head(x), tail(y)  node.Store()
314 //
315 // C. Remove head:
316 //    1. r(r, b), b(r, y), head(r), tail(y)           initial state
317 //    2. r(r, b), b(r, y), head(b), tail(y)           WriteHead()
318 //    3. r(r, b), b(b, y), head(b), tail(y)           next.Store()
319 //    4. r(0, 0), b(b, y), head(b), tail(y)           prev.Store()
320 //
321 // D. Remove tail:
322 //    1. a(x, r), r(a, r), head(x), tail(r)           initial state
323 //    2. a(x, r), r(a, r), head(x), tail(a)           WriteTail()
324 //    3. a(x, a), r(a, r), head(x), tail(a)           prev.Store()
325 //    4. a(x, a), r(0, 0), head(x), tail(a)           next.Store()
Remove(CacheRankingsBlock * node,List list,bool strict)326 void Rankings::Remove(CacheRankingsBlock* node, List list, bool strict) {
327   DCHECK(node->HasData());
328 
329   Addr next_addr(node->Data()->next);
330   Addr prev_addr(node->Data()->prev);
331   if (!next_addr.is_initialized() || next_addr.is_separate_file() ||
332       !prev_addr.is_initialized() || prev_addr.is_separate_file()) {
333     if (next_addr.is_initialized() || prev_addr.is_initialized()) {
334       LOG(ERROR) << "Invalid rankings info.";
335       STRESS_NOTREACHED();
336     }
337     return;
338   }
339 
340   CacheRankingsBlock next(backend_->File(next_addr), next_addr);
341   CacheRankingsBlock prev(backend_->File(prev_addr), prev_addr);
342   if (!GetRanking(&next) || !GetRanking(&prev)) {
343     STRESS_NOTREACHED();
344     return;
345   }
346 
347   if (!CheckLinks(node, &prev, &next, &list))
348     return;
349 
350   Transaction lock(control_data_, node->address(), REMOVE, list);
351   prev.Data()->next = next.address().value();
352   next.Data()->prev = prev.address().value();
353   GenerateCrash(ON_REMOVE_1);
354 
355   CacheAddr node_value = node->address().value();
356   Addr& my_head = heads_[list];
357   Addr& my_tail = tails_[list];
358   if (node_value == my_head.value() || node_value == my_tail.value()) {
359     if (my_head.value() == my_tail.value()) {
360       my_head.set_value(0);
361       my_tail.set_value(0);
362 
363       WriteHead(list);
364       GenerateCrash(ON_REMOVE_2);
365       WriteTail(list);
366       GenerateCrash(ON_REMOVE_3);
367     } else if (node_value == my_head.value()) {
368       my_head.set_value(next.address().value());
369       next.Data()->prev = next.address().value();
370 
371       WriteHead(list);
372       GenerateCrash(ON_REMOVE_4);
373     } else if (node_value == my_tail.value()) {
374       my_tail.set_value(prev.address().value());
375       prev.Data()->next = prev.address().value();
376 
377       WriteTail(list);
378       GenerateCrash(ON_REMOVE_5);
379 
380       // Store the new tail to make sure we can undo the operation if we crash.
381       prev.Store();
382       GenerateCrash(ON_REMOVE_6);
383     }
384   }
385 
386   // Nodes out of the list can be identified by invalid pointers.
387   node->Data()->next = 0;
388   node->Data()->prev = 0;
389 
390   // The last thing to get to disk is the node itself, so before that there is
391   // enough info to recover.
392   next.Store();
393   GenerateCrash(ON_REMOVE_7);
394   prev.Store();
395   GenerateCrash(ON_REMOVE_8);
396   node->Store();
397   DecrementCounter(list);
398   if (strict)
399     UpdateIteratorsForRemoved(node_value, &next);
400 
401   UpdateIterators(&next);
402   UpdateIterators(&prev);
403   backend_->FlushIndex();
404 }
405 
406 // A crash in between Remove and Insert will lead to a dirty entry not on the
407 // list. We want to avoid that case as much as we can (as while waiting for IO),
408 // but the net effect is just an assert on debug when attempting to remove the
409 // entry. Otherwise we'll need reentrant transactions, which is an overkill.
UpdateRank(CacheRankingsBlock * node,bool modified,List list)410 void Rankings::UpdateRank(CacheRankingsBlock* node, bool modified, List list) {
411   Addr& my_head = heads_[list];
412   if (my_head.value() == node->address().value()) {
413     UpdateTimes(node, modified);
414     node->set_modified();
415     return;
416   }
417 
418   TimeTicks start = TimeTicks::Now();
419   Remove(node, list, true);
420   Insert(node, modified, list);
421   CACHE_UMA(AGE_MS, "UpdateRank", 0, start);
422 }
423 
GetNext(CacheRankingsBlock * node,List list)424 CacheRankingsBlock* Rankings::GetNext(CacheRankingsBlock* node, List list) {
425   ScopedRankingsBlock next(this);
426   if (!node) {
427     Addr& my_head = heads_[list];
428     if (!my_head.is_initialized())
429       return nullptr;
430     next.reset(new CacheRankingsBlock(backend_->File(my_head), my_head));
431   } else {
432     if (!node->HasData())
433       node->Load();
434     Addr& my_tail = tails_[list];
435     if (!my_tail.is_initialized())
436       return nullptr;
437     if (my_tail.value() == node->address().value())
438       return nullptr;
439     Addr address(node->Data()->next);
440     if (address.value() == node->address().value())
441       return nullptr;  // Another tail? fail it.
442     next.reset(new CacheRankingsBlock(backend_->File(address), address));
443   }
444 
445   TrackRankingsBlock(next.get(), true);
446 
447   if (!GetRanking(next.get()))
448     return nullptr;
449 
450   ConvertToLongLived(next.get());
451   if (node && !CheckSingleLink(node, next.get()))
452     return nullptr;
453 
454   return next.release();
455 }
456 
GetPrev(CacheRankingsBlock * node,List list)457 CacheRankingsBlock* Rankings::GetPrev(CacheRankingsBlock* node, List list) {
458   ScopedRankingsBlock prev(this);
459   if (!node) {
460     Addr& my_tail = tails_[list];
461     if (!my_tail.is_initialized())
462       return nullptr;
463     prev.reset(new CacheRankingsBlock(backend_->File(my_tail), my_tail));
464   } else {
465     if (!node->HasData())
466       node->Load();
467     Addr& my_head = heads_[list];
468     if (!my_head.is_initialized())
469       return nullptr;
470     if (my_head.value() == node->address().value())
471       return nullptr;
472     Addr address(node->Data()->prev);
473     if (address.value() == node->address().value())
474       return nullptr;  // Another head? fail it.
475     prev.reset(new CacheRankingsBlock(backend_->File(address), address));
476   }
477 
478   TrackRankingsBlock(prev.get(), true);
479 
480   if (!GetRanking(prev.get()))
481     return nullptr;
482 
483   ConvertToLongLived(prev.get());
484   if (node && !CheckSingleLink(prev.get(), node))
485     return nullptr;
486 
487   return prev.release();
488 }
489 
FreeRankingsBlock(CacheRankingsBlock * node)490 void Rankings::FreeRankingsBlock(CacheRankingsBlock* node) {
491   TrackRankingsBlock(node, false);
492 }
493 
TrackRankingsBlock(CacheRankingsBlock * node,bool start_tracking)494 void Rankings::TrackRankingsBlock(CacheRankingsBlock* node,
495                                   bool start_tracking) {
496   if (!node)
497     return;
498 
499   IteratorPair current(node->address().value(), node);
500 
501   if (start_tracking)
502     iterators_.push_back(current);
503   else
504     iterators_.remove(current);
505 }
506 
SelfCheck()507 int Rankings::SelfCheck() {
508   int total = 0;
509   int error = 0;
510   for (int i = 0; i < LAST_ELEMENT; i++) {
511     int partial = CheckList(static_cast<List>(i));
512     if (partial < 0 && !error)
513       error = partial;
514     else if (partial > 0)
515       total += partial;
516   }
517 
518   return error ? error : total;
519 }
520 
SanityCheck(CacheRankingsBlock * node,bool from_list) const521 bool Rankings::SanityCheck(CacheRankingsBlock* node, bool from_list) const {
522   if (!node->VerifyHash())
523     return false;
524 
525   const RankingsNode* data = node->Data();
526 
527   if ((!data->next && data->prev) || (data->next && !data->prev))
528     return false;
529 
530   // Both pointers on zero is a node out of the list.
531   if (!data->next && !data->prev && from_list)
532     return false;
533 
534   List list = NO_USE;  // Initialize it to something.
535   if ((node->address().value() == data->prev) && !IsHead(data->prev, &list))
536     return false;
537 
538   if ((node->address().value() == data->next) && !IsTail(data->next, &list))
539     return false;
540 
541   if (!data->next && !data->prev)
542     return true;
543 
544   Addr next_addr(data->next);
545   Addr prev_addr(data->prev);
546   if (!next_addr.SanityCheck() || next_addr.file_type() != RANKINGS ||
547       !prev_addr.SanityCheck() || prev_addr.file_type() != RANKINGS)
548     return false;
549 
550   return true;
551 }
552 
DataSanityCheck(CacheRankingsBlock * node,bool from_list) const553 bool Rankings::DataSanityCheck(CacheRankingsBlock* node, bool from_list) const {
554   const RankingsNode* data = node->Data();
555   if (!data->contents)
556     return false;
557 
558   // It may have never been inserted.
559   if (from_list && (!data->last_used || !data->last_modified))
560     return false;
561 
562   return true;
563 }
564 
SetContents(CacheRankingsBlock * node,CacheAddr address)565 void Rankings::SetContents(CacheRankingsBlock* node, CacheAddr address) {
566   node->Data()->contents = address;
567   node->Store();
568 }
569 
ReadHeads()570 void Rankings::ReadHeads() {
571   for (int i = 0; i < LAST_ELEMENT; i++)
572     heads_[i] = Addr(control_data_->heads[i]);
573 }
574 
ReadTails()575 void Rankings::ReadTails() {
576   for (int i = 0; i < LAST_ELEMENT; i++)
577     tails_[i] = Addr(control_data_->tails[i]);
578 }
579 
WriteHead(List list)580 void Rankings::WriteHead(List list) {
581   control_data_->heads[list] = heads_[list].value();
582 }
583 
WriteTail(List list)584 void Rankings::WriteTail(List list) {
585   control_data_->tails[list] = tails_[list].value();
586 }
587 
GetRanking(CacheRankingsBlock * rankings)588 bool Rankings::GetRanking(CacheRankingsBlock* rankings) {
589   if (!rankings->address().is_initialized())
590     return false;
591 
592   TimeTicks start = TimeTicks::Now();
593   if (!rankings->Load())
594     return false;
595 
596   if (!SanityCheck(rankings, true)) {
597     backend_->CriticalError(ERR_INVALID_LINKS);
598     return false;
599   }
600 
601   backend_->OnEvent(Stats::OPEN_RANKINGS);
602 
603   // Note that if the cache is in read_only mode, open entries are not marked
604   // as dirty, except when an entry is doomed. We have to look for open entries.
605   if (!backend_->read_only() && !rankings->Data()->dirty)
606     return true;
607 
608   EntryImpl* entry = backend_->GetOpenEntry(rankings);
609   if (!entry) {
610     if (backend_->read_only())
611       return true;
612 
613     // We cannot trust this entry, but we cannot initiate a cleanup from this
614     // point (we may be in the middle of a cleanup already). The entry will be
615     // deleted when detected from a regular open/create path.
616     rankings->Data()->dirty = backend_->GetCurrentEntryId() - 1;
617     if (!rankings->Data()->dirty)
618       rankings->Data()->dirty--;
619     return true;
620   }
621 
622   // Note that we should not leave this module without deleting rankings first.
623   rankings->SetData(entry->rankings()->Data());
624 
625   CACHE_UMA(AGE_MS, "GetRankings", 0, start);
626   return true;
627 }
628 
ConvertToLongLived(CacheRankingsBlock * rankings)629 void Rankings::ConvertToLongLived(CacheRankingsBlock* rankings) {
630   if (rankings->own_data())
631     return;
632 
633   // We cannot return a shared node because we are not keeping a reference
634   // to the entry that owns the buffer. Make this node a copy of the one that
635   // we have, and let the iterator logic update it when the entry changes.
636   CacheRankingsBlock temp(nullptr, Addr(0));
637   *temp.Data() = *rankings->Data();
638   rankings->StopSharingData();
639   *rankings->Data() = *temp.Data();
640 }
641 
CompleteTransaction()642 void Rankings::CompleteTransaction() {
643   Addr node_addr(static_cast<CacheAddr>(control_data_->transaction));
644   if (!node_addr.is_initialized() || node_addr.is_separate_file()) {
645     NOTREACHED();
646     LOG(ERROR) << "Invalid rankings info.";
647     return;
648   }
649 
650   CacheRankingsBlock node(backend_->File(node_addr), node_addr);
651   if (!node.Load())
652     return;
653 
654   node.Store();
655 
656   // We want to leave the node inside the list. The entry must me marked as
657   // dirty, and will be removed later. Otherwise, we'll get assertions when
658   // attempting to remove the dirty entry.
659   if (INSERT == control_data_->operation) {
660     FinishInsert(&node);
661   } else if (REMOVE == control_data_->operation) {
662     RevertRemove(&node);
663   } else {
664     NOTREACHED();
665     LOG(ERROR) << "Invalid operation to recover.";
666   }
667 }
668 
FinishInsert(CacheRankingsBlock * node)669 void Rankings::FinishInsert(CacheRankingsBlock* node) {
670   control_data_->transaction = 0;
671   control_data_->operation = 0;
672   Addr& my_head = heads_[control_data_->operation_list];
673   Addr& my_tail = tails_[control_data_->operation_list];
674   if (my_head.value() != node->address().value()) {
675     if (my_tail.value() == node->address().value()) {
676       // This part will be skipped by the logic of Insert.
677       node->Data()->next = my_tail.value();
678     }
679 
680     Insert(node, true, static_cast<List>(control_data_->operation_list));
681   }
682 
683   // Tell the backend about this entry.
684   backend_->RecoveredEntry(node);
685 }
686 
RevertRemove(CacheRankingsBlock * node)687 void Rankings::RevertRemove(CacheRankingsBlock* node) {
688   Addr next_addr(node->Data()->next);
689   Addr prev_addr(node->Data()->prev);
690   if (!next_addr.is_initialized() || !prev_addr.is_initialized()) {
691     // The operation actually finished. Nothing to do.
692     control_data_->transaction = 0;
693     return;
694   }
695   if (next_addr.is_separate_file() || prev_addr.is_separate_file()) {
696     NOTREACHED();
697     LOG(WARNING) << "Invalid rankings info.";
698     control_data_->transaction = 0;
699     return;
700   }
701 
702   CacheRankingsBlock next(backend_->File(next_addr), next_addr);
703   CacheRankingsBlock prev(backend_->File(prev_addr), prev_addr);
704   if (!next.Load() || !prev.Load())
705     return;
706 
707   CacheAddr node_value = node->address().value();
708   DCHECK(prev.Data()->next == node_value ||
709          prev.Data()->next == prev_addr.value() ||
710          prev.Data()->next == next.address().value());
711   DCHECK(next.Data()->prev == node_value ||
712          next.Data()->prev == next_addr.value() ||
713          next.Data()->prev == prev.address().value());
714 
715   if (node_value != prev_addr.value())
716     prev.Data()->next = node_value;
717   if (node_value != next_addr.value())
718     next.Data()->prev = node_value;
719 
720   List my_list = static_cast<List>(control_data_->operation_list);
721   Addr& my_head = heads_[my_list];
722   Addr& my_tail = tails_[my_list];
723   if (!my_head.is_initialized() || !my_tail.is_initialized()) {
724     my_head.set_value(node_value);
725     my_tail.set_value(node_value);
726     WriteHead(my_list);
727     WriteTail(my_list);
728   } else if (my_head.value() == next.address().value()) {
729     my_head.set_value(node_value);
730     prev.Data()->next = next.address().value();
731     WriteHead(my_list);
732   } else if (my_tail.value() == prev.address().value()) {
733     my_tail.set_value(node_value);
734     next.Data()->prev = prev.address().value();
735     WriteTail(my_list);
736   }
737 
738   next.Store();
739   prev.Store();
740   control_data_->transaction = 0;
741   control_data_->operation = 0;
742   backend_->FlushIndex();
743 }
744 
CheckLinks(CacheRankingsBlock * node,CacheRankingsBlock * prev,CacheRankingsBlock * next,List * list)745 bool Rankings::CheckLinks(CacheRankingsBlock* node, CacheRankingsBlock* prev,
746                           CacheRankingsBlock* next, List* list) {
747   CacheAddr node_addr = node->address().value();
748   if (prev->Data()->next == node_addr &&
749       next->Data()->prev == node_addr) {
750     // A regular linked node.
751     return true;
752   }
753 
754   if (node_addr != prev->address().value() &&
755       node_addr != next->address().value() &&
756       prev->Data()->next == next->address().value() &&
757       next->Data()->prev == prev->address().value()) {
758     // The list is actually ok, node is wrong.
759     node->Data()->next = 0;
760     node->Data()->prev = 0;
761     node->Store();
762     return false;
763   }
764 
765   if (prev->Data()->next == node_addr ||
766       next->Data()->prev == node_addr) {
767     // Only one link is weird, lets double check.
768     if (prev->Data()->next != node_addr && IsHead(node_addr, list))
769       return true;
770 
771     if (next->Data()->prev != node_addr && IsTail(node_addr, list))
772       return true;
773   }
774 
775   LOG(ERROR) << "Inconsistent LRU.";
776   STRESS_NOTREACHED();
777 
778   backend_->CriticalError(ERR_INVALID_LINKS);
779   return false;
780 }
781 
CheckSingleLink(CacheRankingsBlock * prev,CacheRankingsBlock * next)782 bool Rankings::CheckSingleLink(CacheRankingsBlock* prev,
783                                CacheRankingsBlock* next) {
784   if (prev->Data()->next != next->address().value() ||
785       next->Data()->prev != prev->address().value()) {
786     LOG(ERROR) << "Inconsistent LRU.";
787 
788     backend_->CriticalError(ERR_INVALID_LINKS);
789     return false;
790   }
791 
792   return true;
793 }
794 
CheckList(List list)795 int Rankings::CheckList(List list) {
796   Addr last1, last2;
797   int head_items;
798   int rv = CheckListSection(list, last1, last2, true,  // Head to tail.
799                             &last1, &last2, &head_items);
800   if (rv == ERR_NO_ERROR)
801     return head_items;
802 
803   return rv;
804 }
805 
806 // Note that the returned error codes assume a forward walk (from head to tail)
807 // so they have to be adjusted accordingly by the caller. We use two stop values
808 // to be able to detect a corrupt node at the end that is not linked going back.
CheckListSection(List list,Addr end1,Addr end2,bool forward,Addr * last,Addr * second_last,int * num_items)809 int Rankings::CheckListSection(List list, Addr end1, Addr end2, bool forward,
810                                Addr* last, Addr* second_last, int* num_items) {
811   Addr current = forward ? heads_[list] : tails_[list];
812   *last = *second_last = current;
813   *num_items = 0;
814   if (!current.is_initialized())
815     return ERR_NO_ERROR;
816 
817   if (!current.SanityCheckForRankings())
818     return ERR_INVALID_HEAD;
819 
820   std::unique_ptr<CacheRankingsBlock> node;
821   Addr prev_addr(current);
822   do {
823     node =
824         std::make_unique<CacheRankingsBlock>(backend_->File(current), current);
825     node->Load();
826     if (!SanityCheck(node.get(), true))
827       return ERR_INVALID_ENTRY;
828 
829     CacheAddr next = forward ? node->Data()->next : node->Data()->prev;
830     CacheAddr prev = forward ? node->Data()->prev : node->Data()->next;
831 
832     if (prev != prev_addr.value())
833       return ERR_INVALID_PREV;
834 
835     Addr next_addr(next);
836     if (!next_addr.SanityCheckForRankings())
837       return ERR_INVALID_NEXT;
838 
839     prev_addr = current;
840     current = next_addr;
841     *second_last = *last;
842     *last = current;
843     (*num_items)++;
844 
845     if (next_addr == prev_addr) {
846       if (next_addr == (forward ? tails_[list] : heads_[list]))
847         return ERR_NO_ERROR;
848       return ERR_INVALID_TAIL;
849     }
850   } while (current != end1 && current != end2);
851   return ERR_NO_ERROR;
852 }
853 
IsHead(CacheAddr addr,List * list) const854 bool Rankings::IsHead(CacheAddr addr, List* list) const {
855   for (int i = 0; i < LAST_ELEMENT; i++) {
856     if (addr == heads_[i].value()) {
857       *list = static_cast<List>(i);
858       return true;
859     }
860   }
861   return false;
862 }
863 
IsTail(CacheAddr addr,List * list) const864 bool Rankings::IsTail(CacheAddr addr, List* list) const {
865   for (int i = 0; i < LAST_ELEMENT; i++) {
866     if (addr == tails_[i].value()) {
867       *list = static_cast<List>(i);
868       return true;
869     }
870   }
871   return false;
872 }
873 
874 // We expect to have just a few iterators at any given time, maybe two or three,
875 // But we could have more than one pointing at the same mode. We walk the list
876 // of cache iterators and update all that are pointing to the given node.
UpdateIterators(CacheRankingsBlock * node)877 void Rankings::UpdateIterators(CacheRankingsBlock* node) {
878   CacheAddr address = node->address().value();
879   for (auto& iterator : iterators_) {
880     if (iterator.first == address && iterator.second->HasData()) {
881       CacheRankingsBlock* other = iterator.second;
882       if (other != node)
883         *other->Data() = *node->Data();
884     }
885   }
886 }
887 
UpdateIteratorsForRemoved(CacheAddr address,CacheRankingsBlock * next)888 void Rankings::UpdateIteratorsForRemoved(CacheAddr address,
889                                          CacheRankingsBlock* next) {
890   CacheAddr next_addr = next->address().value();
891   for (auto& iterator : iterators_) {
892     if (iterator.first == address) {
893       iterator.first = next_addr;
894       iterator.second->CopyFrom(next);
895     }
896   }
897 }
898 
IncrementCounter(List list)899 void Rankings::IncrementCounter(List list) {
900   if (!count_lists_)
901     return;
902 
903   DCHECK(control_data_->sizes[list] < std::numeric_limits<int32_t>::max());
904   if (control_data_->sizes[list] < std::numeric_limits<int32_t>::max())
905     control_data_->sizes[list]++;
906 }
907 
DecrementCounter(List list)908 void Rankings::DecrementCounter(List list) {
909   if (!count_lists_)
910     return;
911 
912   DCHECK(control_data_->sizes[list] > 0);
913   if (control_data_->sizes[list] > 0)
914     control_data_->sizes[list]--;
915 }
916 
917 }  // namespace disk_cache
918