]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SwapDir.cc
33b5a4dadae6ce06e706412a3570c560c2487a86
[thirdparty/squid.git] / src / SwapDir.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 20 Swap Dir base object
5 * AUTHOR: Robert Collins
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "compat/strtoll.h"
37 #include "SwapDir.h"
38 #include "StoreFileSystem.h"
39 #include "ConfigOption.h"
40
41 SwapDir::SwapDir(char const *aType): theType(aType),
42 max_size(0),
43 path(NULL), index(-1), disker(-1), min_objsize(0), max_objsize (-1),
44 repl(NULL), removals(0), scanned(0),
45 cleanLog(NULL)
46 {
47 fs.blksize = 1024;
48 }
49
50 SwapDir::~SwapDir()
51 {
52 // TODO: should we delete repl?
53 xfree(path);
54 }
55
56 void
57 SwapDir::create() {}
58
59 void
60 SwapDir::dump(StoreEntry &)const {}
61
62 bool
63 SwapDir::doubleCheck(StoreEntry &)
64 {
65 return false;
66 }
67
68 void
69 SwapDir::unlink(StoreEntry &) {}
70
71 void
72 SwapDir::getStats(StoreInfoStats &stats) const
73 {
74 if (!doReportStat())
75 return;
76
77 stats.swap.size = currentSize();
78 stats.swap.capacity = maxSize();
79 stats.swap.count = currentCount();
80 }
81
82 void
83 SwapDir::stat(StoreEntry &output) const
84 {
85 if (!doReportStat())
86 return;
87
88 storeAppendPrintf(&output, "Store Directory #%d (%s): %s\n", index, type(),
89 path);
90 storeAppendPrintf(&output, "FS Block Size %d Bytes\n",
91 fs.blksize);
92 statfs(output);
93
94 if (repl) {
95 storeAppendPrintf(&output, "Removal policy: %s\n", repl->_type);
96
97 if (repl->Stats)
98 repl->Stats(repl, &output);
99 }
100 }
101
102 void
103 SwapDir::statfs(StoreEntry &)const {}
104
105 void
106 SwapDir::maintain() {}
107
108 uint64_t
109 SwapDir::minSize() const
110 {
111 return ((maxSize() * Config.Swap.lowWaterMark) / 100);
112 }
113
114 void
115 SwapDir::reference(StoreEntry &) {}
116
117 bool
118 SwapDir::dereference(StoreEntry &)
119 {
120 return true; // keep in global store_table
121 }
122
123 int
124 SwapDir::callback()
125 {
126 return 0;
127 }
128
129 bool
130 SwapDir::canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const
131 {
132 debugs(47,8, HERE << "cache_dir[" << index << "]: needs " <<
133 diskSpaceNeeded << " <? " << max_objsize);
134
135 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
136 return false; // we do not store Squid-generated entries
137
138 if (!objectSizeIsAcceptable(diskSpaceNeeded))
139 return false; // does not satisfy size limits
140
141 if (flags.read_only)
142 return false; // cannot write at all
143
144 if (currentSize() > maxSize())
145 return false; // already overflowing
146
147 /* Return 999 (99.9%) constant load; TODO: add a named constant for this */
148 load = 999;
149 return true; // kids may provide more tests and should report true load
150 }
151
152
153 void
154 SwapDir::sync() {}
155
156 /* Move to StoreEntry ? */
157 bool
158 SwapDir::canLog(StoreEntry const &e)const
159 {
160 if (e.swap_filen < 0)
161 return false;
162
163 if (e.swap_status != SWAPOUT_DONE)
164 return false;
165
166 if (e.swap_file_sz <= 0)
167 return false;
168
169 if (EBIT_TEST(e.flags, RELEASE_REQUEST))
170 return false;
171
172 if (EBIT_TEST(e.flags, KEY_PRIVATE))
173 return false;
174
175 if (EBIT_TEST(e.flags, ENTRY_SPECIAL))
176 return false;
177
178 return true;
179 }
180
181 void
182 SwapDir::openLog() {}
183
184 void
185 SwapDir::closeLog() {}
186
187 int
188 SwapDir::writeCleanStart()
189 {
190 return 0;
191 }
192
193 void
194 SwapDir::writeCleanDone() {}
195
196 void
197 SwapDir::logEntry(const StoreEntry & e, int op) const {}
198
199 char const *
200 SwapDir::type() const
201 {
202 return theType;
203 }
204
205 bool
206 SwapDir::active() const
207 {
208 if (IamWorkerProcess())
209 return true;
210
211 // we are inside a disker dedicated to this disk
212 if (KidIdentifier == disker)
213 return true;
214
215 return false; // Coordinator, wrong disker, etc.
216 }
217
218 bool
219 SwapDir::needsDiskStrand() const
220 {
221 return false;
222 }
223
224 /* NOT performance critical. Really. Don't bother optimising for speed
225 * - RBC 20030718
226 */
227 ConfigOption *
228 SwapDir::getOptionTree() const
229 {
230 ConfigOptionVector *result = new ConfigOptionVector;
231 result->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::optionReadOnlyParse, &SwapDir::optionReadOnlyDump));
232 result->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::optionObjectSizeParse, &SwapDir::optionObjectSizeDump));
233 return result;
234 }
235
236 void
237 SwapDir::parseOptions(int isaReconfig)
238 {
239 unsigned int old_read_only = flags.read_only;
240 char *name, *value;
241
242 ConfigOption *newOption = getOptionTree();
243
244 while ((name = strtok(NULL, w_space)) != NULL) {
245 value = strchr(name, '=');
246
247 if (value)
248 *value++ = '\0'; /* cut on = */
249
250 debugs(3,2, "SwapDir::parseOptions: parsing store option '" << name << "'='" << (value ? value : "") << "'");
251
252 if (newOption)
253 if (!newOption->parse(name, value, isaReconfig))
254 self_destruct();
255 }
256
257 delete newOption;
258
259 /*
260 * Handle notifications about reconfigured single-options with no value
261 * where the removal of the option cannot be easily detected in the
262 * parsing...
263 */
264
265 if (isaReconfig) {
266 if (old_read_only != flags.read_only) {
267 debugs(3, 1, "Cache dir '" << path << "' now " << (flags.read_only ? "No-Store" : "Read-Write"));
268 }
269 }
270 }
271
272 void
273 SwapDir::dumpOptions(StoreEntry * entry) const
274 {
275 ConfigOption *newOption = getOptionTree();
276
277 if (newOption)
278 newOption->dump(entry);
279
280 delete newOption;
281 }
282
283 bool
284 SwapDir::optionReadOnlyParse(char const *option, const char *value, int isaReconfig)
285 {
286 if (strcmp(option, "no-store") != 0 && strcmp(option, "read-only") != 0)
287 return false;
288
289 int read_only = 0;
290
291 if (value)
292 read_only = xatoi(value);
293 else
294 read_only = 1;
295
296 flags.read_only = read_only;
297
298 return true;
299 }
300
301 void
302 SwapDir::optionReadOnlyDump(StoreEntry * e) const
303 {
304 if (flags.read_only)
305 storeAppendPrintf(e, " no-store");
306 }
307
308 bool
309 SwapDir::optionObjectSizeParse(char const *option, const char *value, int isaReconfig)
310 {
311 int64_t *val;
312 if (strcmp(option, "max-size") == 0) {
313 val = &max_objsize;
314 } else if (strcmp(option, "min-size") == 0) {
315 val = &min_objsize;
316 } else
317 return false;
318
319 if (!value)
320 self_destruct();
321
322 int64_t size = strtoll(value, NULL, 10);
323
324 if (isaReconfig && *val != size) {
325 if (allowOptionReconfigure(option)) {
326 debugs(3, DBG_IMPORTANT, "cache_dir '" << path << "' object " <<
327 option << " now " << size << " Bytes");
328 } else {
329 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir '" << path << "' "
330 "object " << option << " cannot be changed dynamically, " <<
331 "value left unchanged (" << *val << " Bytes)");
332 return true;
333 }
334 }
335
336 *val = size;
337
338 return true;
339 }
340
341 void
342 SwapDir::optionObjectSizeDump(StoreEntry * e) const
343 {
344 if (min_objsize != 0)
345 storeAppendPrintf(e, " min-size=%"PRId64, min_objsize);
346
347 if (max_objsize != -1)
348 storeAppendPrintf(e, " max-size=%"PRId64, max_objsize);
349 }
350
351 // some SwapDirs may maintain their indexes and be able to lookup an entry key
352 StoreEntry *
353 SwapDir::get(const cache_key *key)
354 {
355 return NULL;
356 }
357
358 void
359 SwapDir::get(String const key, STOREGETCLIENT aCallback, void *aCallbackData)
360 {
361 fatal("not implemented");
362 }