]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SwapDir.cc
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / SwapDir.cc
1 /*
2 * Copyright (C) 1996-2014 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 /* DEBUG: section 20 Swap Dir base object */
10
11 #include "squid.h"
12 #include "cache_cf.h"
13 #include "compat/strtoll.h"
14 #include "ConfigOption.h"
15 #include "ConfigParser.h"
16 #include "globals.h"
17 #include "Parsing.h"
18 #include "SquidConfig.h"
19 #include "StoreFileSystem.h"
20 #include "SwapDir.h"
21 #include "tools.h"
22
23 SwapDir::SwapDir(char const *aType): theType(aType),
24 max_size(0), min_objsize(0), max_objsize (-1),
25 path(NULL), index(-1), disker(-1),
26 repl(NULL), removals(0), scanned(0),
27 cleanLog(NULL)
28 {
29 fs.blksize = 1024;
30 }
31
32 SwapDir::~SwapDir()
33 {
34 // TODO: should we delete repl?
35 xfree(path);
36 }
37
38 void
39 SwapDir::create() {}
40
41 void
42 SwapDir::dump(StoreEntry &)const {}
43
44 bool
45 SwapDir::doubleCheck(StoreEntry &)
46 {
47 return false;
48 }
49
50 void
51 SwapDir::unlink(StoreEntry &) {}
52
53 void
54 SwapDir::getStats(StoreInfoStats &stats) const
55 {
56 if (!doReportStat())
57 return;
58
59 stats.swap.size = currentSize();
60 stats.swap.capacity = maxSize();
61 stats.swap.count = currentCount();
62 }
63
64 void
65 SwapDir::stat(StoreEntry &output) const
66 {
67 if (!doReportStat())
68 return;
69
70 storeAppendPrintf(&output, "Store Directory #%d (%s): %s\n", index, type(),
71 path);
72 storeAppendPrintf(&output, "FS Block Size %d Bytes\n",
73 fs.blksize);
74 statfs(output);
75
76 if (repl) {
77 storeAppendPrintf(&output, "Removal policy: %s\n", repl->_type);
78
79 if (repl->Stats)
80 repl->Stats(repl, &output);
81 }
82 }
83
84 void
85 SwapDir::statfs(StoreEntry &)const {}
86
87 void
88 SwapDir::maintain() {}
89
90 uint64_t
91 SwapDir::minSize() const
92 {
93 return ((maxSize() * Config.Swap.lowWaterMark) / 100);
94 }
95
96 int64_t
97 SwapDir::maxObjectSize() const
98 {
99 // per-store max-size=N value is authoritative
100 if (max_objsize > -1)
101 return max_objsize;
102
103 // store with no individual max limit is limited by configured maximum_object_size
104 // or the total store size, whichever is smaller
105 return min(static_cast<int64_t>(maxSize()), Config.Store.maxObjectSize);
106 }
107
108 void
109 SwapDir::maxObjectSize(int64_t newMax)
110 {
111 // negative values mean no limit (-1)
112 if (newMax < 0) {
113 max_objsize = -1; // set explicitly in case it had a non-default value previously
114 return;
115 }
116
117 // prohibit values greater than total storage area size
118 // but set max_objsize to the maximum allowed to override maximum_object_size global config
119 if (static_cast<uint64_t>(newMax) > maxSize()) {
120 debugs(47, DBG_PARSE_NOTE(2), "WARNING: Ignoring 'max-size' option for " << path <<
121 " which is larger than total cache_dir size of " << maxSize() << " bytes.");
122 max_objsize = maxSize();
123 return;
124 }
125
126 max_objsize = newMax;
127 }
128
129 void
130 SwapDir::reference(StoreEntry &) {}
131
132 bool
133 SwapDir::dereference(StoreEntry &, bool)
134 {
135 return true; // keep in global store_table
136 }
137
138 int
139 SwapDir::callback()
140 {
141 return 0;
142 }
143
144 bool
145 SwapDir::canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const
146 {
147 debugs(47,8, HERE << "cache_dir[" << index << "]: needs " <<
148 diskSpaceNeeded << " <? " << max_objsize);
149
150 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
151 return false; // we do not store Squid-generated entries
152
153 if (!objectSizeIsAcceptable(diskSpaceNeeded))
154 return false; // does not satisfy size limits
155
156 if (flags.read_only)
157 return false; // cannot write at all
158
159 if (currentSize() > maxSize())
160 return false; // already overflowing
161
162 /* Return 999 (99.9%) constant load; TODO: add a named constant for this */
163 load = 999;
164 return true; // kids may provide more tests and should report true load
165 }
166
167 void
168 SwapDir::sync() {}
169
170 /* Move to StoreEntry ? */
171 bool
172 SwapDir::canLog(StoreEntry const &e)const
173 {
174 if (e.swap_filen < 0)
175 return false;
176
177 if (e.swap_status != SWAPOUT_DONE)
178 return false;
179
180 if (e.swap_file_sz <= 0)
181 return false;
182
183 if (EBIT_TEST(e.flags, RELEASE_REQUEST))
184 return false;
185
186 if (EBIT_TEST(e.flags, KEY_PRIVATE))
187 return false;
188
189 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
190 return false;
191
192 return true;
193 }
194
195 void
196 SwapDir::openLog() {}
197
198 void
199 SwapDir::closeLog() {}
200
201 int
202 SwapDir::writeCleanStart()
203 {
204 return 0;
205 }
206
207 void
208 SwapDir::writeCleanDone() {}
209
210 void
211 SwapDir::logEntry(const StoreEntry & e, int op) const {}
212
213 char const *
214 SwapDir::type() const
215 {
216 return theType;
217 }
218
219 bool
220 SwapDir::active() const
221 {
222 if (IamWorkerProcess())
223 return true;
224
225 // we are inside a disker dedicated to this disk
226 if (KidIdentifier == disker)
227 return true;
228
229 return false; // Coordinator, wrong disker, etc.
230 }
231
232 bool
233 SwapDir::needsDiskStrand() const
234 {
235 return false;
236 }
237
238 /* NOT performance critical. Really. Don't bother optimising for speed
239 * - RBC 20030718
240 */
241 ConfigOption *
242 SwapDir::getOptionTree() const
243 {
244 ConfigOptionVector *result = new ConfigOptionVector;
245 result->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::optionReadOnlyParse, &SwapDir::optionReadOnlyDump));
246 result->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::optionObjectSizeParse, &SwapDir::optionObjectSizeDump));
247 return result;
248 }
249
250 void
251 SwapDir::parseOptions(int isaReconfig)
252 {
253 const bool old_read_only = flags.read_only;
254 char *name, *value;
255
256 ConfigOption *newOption = getOptionTree();
257
258 while ((name = ConfigParser::NextToken()) != NULL) {
259 value = strchr(name, '=');
260
261 if (value) {
262 *value = '\0'; /* cut on = */
263 ++value;
264 }
265
266 debugs(3,2, "SwapDir::parseOptions: parsing store option '" << name << "'='" << (value ? value : "") << "'");
267
268 if (newOption)
269 if (!newOption->parse(name, value, isaReconfig))
270 self_destruct();
271 }
272
273 delete newOption;
274
275 /*
276 * Handle notifications about reconfigured single-options with no value
277 * where the removal of the option cannot be easily detected in the
278 * parsing...
279 */
280
281 if (isaReconfig) {
282 if (old_read_only != flags.read_only) {
283 debugs(3, DBG_IMPORTANT, "Cache dir '" << path << "' now " << (flags.read_only ? "No-Store" : "Read-Write"));
284 }
285 }
286 }
287
288 void
289 SwapDir::dumpOptions(StoreEntry * entry) const
290 {
291 ConfigOption *newOption = getOptionTree();
292
293 if (newOption)
294 newOption->dump(entry);
295
296 delete newOption;
297 }
298
299 bool
300 SwapDir::optionReadOnlyParse(char const *option, const char *value, int isaReconfig)
301 {
302 if (strcmp(option, "no-store") != 0 && strcmp(option, "read-only") != 0)
303 return false;
304
305 if (strcmp(option, "read-only") == 0) {
306 debugs(3, DBG_PARSE_NOTE(3), "UPGRADE WARNING: Replace cache_dir option 'read-only' with 'no-store'.");
307 }
308
309 bool read_only = 0;
310
311 if (value)
312 read_only = (xatoi(value) != 0);
313 else
314 read_only = true;
315
316 flags.read_only = read_only;
317
318 return true;
319 }
320
321 void
322 SwapDir::optionReadOnlyDump(StoreEntry * e) const
323 {
324 if (flags.read_only)
325 storeAppendPrintf(e, " no-store");
326 }
327
328 bool
329 SwapDir::optionObjectSizeParse(char const *option, const char *value, int isaReconfig)
330 {
331 int64_t *val;
332 if (strcmp(option, "max-size") == 0) {
333 val = &max_objsize;
334 } else if (strcmp(option, "min-size") == 0) {
335 val = &min_objsize;
336 } else
337 return false;
338
339 if (!value)
340 self_destruct();
341
342 int64_t size = strtoll(value, NULL, 10);
343
344 if (isaReconfig && *val != size) {
345 if (allowOptionReconfigure(option)) {
346 debugs(3, DBG_IMPORTANT, "cache_dir '" << path << "' object " <<
347 option << " now " << size << " Bytes");
348 } else {
349 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir '" << path << "' "
350 "object " << option << " cannot be changed dynamically, " <<
351 "value left unchanged (" << *val << " Bytes)");
352 return true;
353 }
354 }
355
356 *val = size;
357
358 return true;
359 }
360
361 void
362 SwapDir::optionObjectSizeDump(StoreEntry * e) const
363 {
364 if (min_objsize != 0)
365 storeAppendPrintf(e, " min-size=%" PRId64, min_objsize);
366
367 if (max_objsize != -1)
368 storeAppendPrintf(e, " max-size=%" PRId64, max_objsize);
369 }
370
371 // some SwapDirs may maintain their indexes and be able to lookup an entry key
372 StoreEntry *
373 SwapDir::get(const cache_key *key)
374 {
375 return NULL;
376 }
377
378 void
379 SwapDir::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
380 {
381 fatal("not implemented");
382 }