]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockSwapDir.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / fs / rock / RockSwapDir.h
1 /*
2 * Copyright (C) 1996-2020 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_SWAP_DIR_H
10 #define SQUID_FS_ROCK_SWAP_DIR_H
11
12 #include "DiskIO/DiskFile.h"
13 #include "DiskIO/IORequestor.h"
14 #include "fs/rock/forward.h"
15 #include "fs/rock/RockDbCell.h"
16 #include "ipc/mem/Page.h"
17 #include "ipc/mem/PageStack.h"
18 #include "ipc/StoreMap.h"
19 #include "store/Disk.h"
20 #include <vector>
21
22 class DiskIOStrategy;
23 class ReadRequest;
24 class WriteRequest;
25
26 namespace Rock
27 {
28
29 /// \ingroup Rock
30 class SwapDir: public ::SwapDir, public IORequestor, public Ipc::StoreMapCleaner
31 {
32 public:
33 typedef RefCount<SwapDir> Pointer;
34 typedef Ipc::StoreMap DirMap;
35
36 SwapDir();
37 virtual ~SwapDir();
38
39 /* public ::SwapDir API */
40 virtual void reconfigure();
41 virtual StoreEntry *get(const cache_key *key);
42 virtual void evictCached(StoreEntry &);
43 virtual void evictIfFound(const cache_key *);
44 virtual void disconnect(StoreEntry &e);
45 virtual uint64_t currentSize() const;
46 virtual uint64_t currentCount() const;
47 virtual bool doReportStat() const;
48 virtual void finalizeSwapoutSuccess(const StoreEntry &);
49 virtual void finalizeSwapoutFailure(StoreEntry &);
50 virtual void create();
51 virtual void parse(int index, char *path);
52 virtual bool smpAware() const { return true; }
53 virtual bool hasReadableEntry(const StoreEntry &) const;
54
55 // temporary path to the shared memory map of first slots of cached entries
56 SBuf inodeMapPath() const;
57 // temporary path to the shared memory stack of free slots
58 const char *freeSlotsPath() const;
59
60 int64_t entryLimitAbsolute() const { return SwapFilenMax+1; } ///< Core limit
61 int64_t entryLimitActual() const; ///< max number of possible entries in db
62 int64_t slotLimitAbsolute() const; ///< Rock store implementation limit
63 int64_t slotLimitActual() const; ///< total number of slots in this db
64
65 /// whether the given slot ID may point to a slot in this db
66 bool validSlotId(const SlotId slotId) const;
67
68 /// finds and returns a free db slot to fill or throws
69 SlotId reserveSlotForWriting();
70
71 /// purges one or more entries to make full() false and free some slots
72 void purgeSome();
73
74 int64_t diskOffset(Ipc::Mem::PageId &pageId) const;
75 int64_t diskOffset(int filen) const;
76 void writeError(StoreIOState &sio);
77
78 /* StoreMapCleaner API */
79 virtual void noteFreeMapSlice(const Ipc::StoreMapSliceId fileno);
80
81 uint64_t slotSize; ///< all db slots are of this size
82
83 protected:
84 /* Store API */
85 virtual bool anchorToCache(StoreEntry &entry, bool &inSync);
86 virtual bool updateAnchored(StoreEntry &);
87
88 /* protected ::SwapDir API */
89 virtual bool needsDiskStrand() const;
90 virtual void init();
91 virtual ConfigOption *getOptionTree() const;
92 virtual bool allowOptionReconfigure(const char *const option) const;
93 virtual bool canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const;
94 virtual StoreIOState::Pointer createStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
95 virtual StoreIOState::Pointer openStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
96 virtual void maintain();
97 virtual void diskFull();
98 virtual void reference(StoreEntry &e);
99 virtual bool dereference(StoreEntry &e);
100 virtual void updateHeaders(StoreEntry *e);
101 virtual bool unlinkdUseful() const;
102 virtual void statfs(StoreEntry &e) const;
103
104 /* IORequestor API */
105 virtual void ioCompletedNotification();
106 virtual void closeCompleted();
107 virtual void readCompleted(const char *buf, int len, int errflag, RefCount< ::ReadRequest>);
108 virtual void writeCompleted(int errflag, size_t len, RefCount< ::WriteRequest>);
109
110 void parseSize(const bool reconfiguring); ///< parses anonymous cache_dir size option
111 void validateOptions(); ///< warns of configuration problems; may quit
112 bool parseTimeOption(char const *option, const char *value, int reconfiguring);
113 void dumpTimeOption(StoreEntry * e) const;
114 bool parseRateOption(char const *option, const char *value, int reconfiguring);
115 void dumpRateOption(StoreEntry * e) const;
116 bool parseSizeOption(char const *option, const char *value, int reconfiguring);
117 void dumpSizeOption(StoreEntry * e) const;
118
119 void rebuild(); ///< starts loading and validating stored entry metadata
120
121 bool full() const; ///< no more entries can be stored without purging
122 void trackReferences(StoreEntry &e); ///< add to replacement policy scope
123 void ignoreReferences(StoreEntry &e); ///< delete from repl policy scope
124
125 int64_t diskOffsetLimit() const;
126
127 void updateHeadersOrThrow(Ipc::StoreMapUpdate &update);
128 StoreIOState::Pointer createUpdateIO(const Ipc::StoreMapUpdate &update, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
129
130 void anchorEntry(StoreEntry &e, const sfileno filen, const Ipc::StoreMapAnchor &anchor);
131 bool updateAnchoredWith(StoreEntry &, const Ipc::StoreMapAnchor &);
132
133 friend class Rebuild;
134 friend class IoState;
135 friend class HeaderUpdater;
136 const char *filePath; ///< location of cache storage file inside path/
137 DirMap *map; ///< entry key/sfileno to MaxExtras/inode mapping
138
139 private:
140 void createError(const char *const msg);
141 void handleWriteCompletionSuccess(const WriteRequest &request);
142 void handleWriteCompletionProblem(const int errflag, const WriteRequest &request);
143
144 DiskIOStrategy *io;
145 RefCount<DiskFile> theFile; ///< cache storage for this cache_dir
146 Ipc::Mem::Pointer<Ipc::Mem::PageStack> freeSlots; ///< all unused slots
147 Ipc::Mem::PageId *waitingForPage; ///< one-page cache for a "hot" free slot
148
149 /* configurable options */
150 DiskFile::Config fileConfig; ///< file-level configuration options
151
152 static const int64_t HeaderSize; ///< on-disk db header size
153 };
154
155 /// initializes shared memory segments used by Rock::SwapDir
156 class SwapDirRr: public Ipc::Mem::RegisteredRunner
157 {
158 public:
159 /* ::RegisteredRunner API */
160 virtual ~SwapDirRr();
161
162 protected:
163 /* Ipc::Mem::RegisteredRunner API */
164 virtual void create();
165
166 private:
167 std::vector<SwapDir::DirMap::Owner *> mapOwners;
168 std::vector< Ipc::Mem::Owner<Ipc::Mem::PageStack> *> freeSlotsOwners;
169 };
170
171 } // namespace Rock
172
173 #endif /* SQUID_FS_ROCK_SWAP_DIR_H */
174