1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_FS_ROCK_REBUILD_H
10 #define SQUID_FS_ROCK_REBUILD_H
11 
12 #include "base/AsyncJob.h"
13 #include "cbdata.h"
14 #include "fs/rock/forward.h"
15 #include "MemBuf.h"
16 #include "store_rebuild.h"
17 
18 namespace Rock
19 {
20 
21 class LoadingEntry;
22 class LoadingSlot;
23 class LoadingParts;
24 
25 /// \ingroup Rock
26 /// manages store rebuild process: loading meta information from db on disk
27 class Rebuild: public AsyncJob
28 {
29     CBDATA_CHILD(Rebuild);
30 
31 public:
32     Rebuild(SwapDir *dir);
33     virtual ~Rebuild() override;
34 
35 protected:
36     /* AsyncJob API */
37     virtual void start() override;
38     virtual bool doneAll() const override;
39     virtual void swanSong() override;
40 
41     bool doneLoading() const;
42     bool doneValidating() const;
43 
44 private:
45     void checkpoint();
46     void steps();
47     void loadingSteps();
48     void validationSteps();
49     void loadOneSlot();
50     void validateOneEntry(const sfileno fileNo);
51     void validateOneSlot(const SlotId slotId);
52     bool importEntry(Ipc::StoreMapAnchor &anchor, const sfileno slotId, const DbCellHeader &header);
53     void freeBadEntry(const sfileno fileno, const char *eDescription);
54 
55     void failure(const char *msg, int errNo = 0);
56 
57     LoadingEntry loadingEntry(const sfileno fileNo);
58     void startNewEntry(const sfileno fileno, const SlotId slotId, const DbCellHeader &header);
59     void primeNewEntry(Ipc::StoreMapAnchor &anchor, const sfileno fileno, const DbCellHeader &header);
60     void finalizeOrFree(const sfileno fileNo, LoadingEntry &le);
61     void finalizeOrThrow(const sfileno fileNo, LoadingEntry &le);
62     void addSlotToEntry(const sfileno fileno, const SlotId slotId, const DbCellHeader &header);
63     void useNewSlot(const SlotId slotId, const DbCellHeader &header);
64 
65     LoadingSlot loadingSlot(const SlotId slotId);
66     void mapSlot(const SlotId slotId, const DbCellHeader &header);
67     void freeUnusedSlot(const SlotId slotId, const bool invalid);
68     void freeSlot(const SlotId slotId, const bool invalid);
69 
70     template <class SlotIdType>
71     void chainSlots(SlotIdType &from, const SlotId to);
72 
73     bool sameEntry(const sfileno fileno, const DbCellHeader &header) const;
74 
75     SwapDir *sd;
76     LoadingParts *parts; ///< parts of store entries being loaded from disk
77 
78     int64_t dbSize;
79     int dbSlotSize; ///< the size of a db cell, including the cell header
80     int dbSlotLimit; ///< total number of db cells
81     int dbEntryLimit; ///< maximum number of entries that can be stored in db
82 
83     int fd; // store db file descriptor
84     int64_t dbOffset;
85     sfileno loadingPos; ///< index of the db slot being loaded from disk now
86     sfileno validationPos; ///< index of the loaded db slot being validated now
87     MemBuf buf; ///< space to load current db slot (and entry metadata) into
88 
89     StoreRebuildData counts;
90 
91     static void Steps(void *data);
92 };
93 
94 } // namespace Rock
95 
96 #endif /* SQUID_FS_ROCK_REBUILD_H */
97 
98