]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/rock/RockSwapDir.cc
Merged from trunk
[thirdparty/squid.git] / src / fs / rock / RockSwapDir.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 47 Store Directory Routines */
10
11 #include "squid.h"
12 #include "cache_cf.h"
13 #include "CollapsedForwarding.h"
14 #include "ConfigOption.h"
15 #include "DiskIO/DiskIOModule.h"
16 #include "DiskIO/DiskIOStrategy.h"
17 #include "DiskIO/ReadRequest.h"
18 #include "DiskIO/WriteRequest.h"
19 #include "fs/rock/RockIoRequests.h"
20 #include "fs/rock/RockIoState.h"
21 #include "fs/rock/RockRebuild.h"
22 #include "fs/rock/RockSwapDir.h"
23 #include "globals.h"
24 #include "ipc/mem/Pages.h"
25 #include "MemObject.h"
26 #include "Parsing.h"
27 #include "SquidConfig.h"
28 #include "SquidMath.h"
29 #include "tools.h"
30
31 #include <cstdlib>
32 #include <iomanip>
33 #include <limits>
34
35 #if HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38
39 const int64_t Rock::SwapDir::HeaderSize = 16*1024;
40
41 Rock::SwapDir::SwapDir(): ::SwapDir("rock"),
42 slotSize(HeaderSize), filePath(NULL), map(NULL), io(NULL),
43 waitingForPage(NULL)
44 {
45 }
46
47 Rock::SwapDir::~SwapDir()
48 {
49 delete io;
50 delete map;
51 safe_free(filePath);
52 }
53
54 StoreSearch *
55 Rock::SwapDir::search(String const, HttpRequest *)
56 {
57 assert(false);
58 return NULL; // XXX: implement
59 }
60
61 void
62 Rock::SwapDir::get(String const key, STOREGETCLIENT cb, void *data)
63 {
64 ::SwapDir::get(key, cb, data);
65 }
66
67 // called when Squid core needs a StoreEntry with a given key
68 StoreEntry *
69 Rock::SwapDir::get(const cache_key *key)
70 {
71 if (!map || !theFile || !theFile->canRead())
72 return NULL;
73
74 sfileno filen;
75 const Ipc::StoreMapAnchor *const slot = map->openForReading(key, filen);
76 if (!slot)
77 return NULL;
78
79 // create a brand new store entry and initialize it with stored basics
80 StoreEntry *e = new StoreEntry();
81 anchorEntry(*e, filen, *slot);
82
83 e->hashInsert(key);
84 trackReferences(*e);
85
86 return e;
87 // the disk entry remains open for reading, protected from modifications
88 }
89
90 bool
91 Rock::SwapDir::anchorCollapsed(StoreEntry &collapsed, bool &inSync)
92 {
93 if (!map || !theFile || !theFile->canRead())
94 return false;
95
96 sfileno filen;
97 const Ipc::StoreMapAnchor *const slot = map->openForReading(
98 reinterpret_cast<cache_key*>(collapsed.key), filen);
99 if (!slot)
100 return false;
101
102 anchorEntry(collapsed, filen, *slot);
103 inSync = updateCollapsedWith(collapsed, *slot);
104 return true; // even if inSync is false
105 }
106
107 bool
108 Rock::SwapDir::updateCollapsed(StoreEntry &collapsed)
109 {
110 if (!map || !theFile || !theFile->canRead())
111 return false;
112
113 if (collapsed.swap_filen < 0) // no longer using a disk cache
114 return true;
115 assert(collapsed.swap_dirn == index);
116
117 const Ipc::StoreMapAnchor &s = map->readableEntry(collapsed.swap_filen);
118 return updateCollapsedWith(collapsed, s);
119 }
120
121 bool
122 Rock::SwapDir::updateCollapsedWith(StoreEntry &collapsed, const Ipc::StoreMapAnchor &anchor)
123 {
124 collapsed.swap_file_sz = anchor.basics.swap_file_sz;
125 return true;
126 }
127
128 void
129 Rock::SwapDir::anchorEntry(StoreEntry &e, const sfileno filen, const Ipc::StoreMapAnchor &anchor)
130 {
131 const Ipc::StoreMapAnchor::Basics &basics = anchor.basics;
132
133 e.swap_file_sz = basics.swap_file_sz;
134 e.lastref = basics.lastref;
135 e.timestamp = basics.timestamp;
136 e.expires = basics.expires;
137 e.lastmod = basics.lastmod;
138 e.refcount = basics.refcount;
139 e.flags = basics.flags;
140
141 if (anchor.complete()) {
142 e.store_status = STORE_OK;
143 e.swap_status = SWAPOUT_DONE;
144 } else {
145 e.store_status = STORE_PENDING;
146 e.swap_status = SWAPOUT_WRITING; // even though another worker writes?
147 }
148
149 e.ping_status = PING_NONE;
150
151 EBIT_CLR(e.flags, RELEASE_REQUEST);
152 EBIT_CLR(e.flags, KEY_PRIVATE);
153 EBIT_SET(e.flags, ENTRY_VALIDATED);
154
155 e.swap_dirn = index;
156 e.swap_filen = filen;
157 }
158
159 void Rock::SwapDir::disconnect(StoreEntry &e)
160 {
161 assert(e.swap_dirn == index);
162 assert(e.swap_filen >= 0);
163 // cannot have SWAPOUT_NONE entry with swap_filen >= 0
164 assert(e.swap_status != SWAPOUT_NONE);
165
166 // do not rely on e.swap_status here because there is an async delay
167 // before it switches from SWAPOUT_WRITING to SWAPOUT_DONE.
168
169 // since e has swap_filen, its slot is locked for reading and/or writing
170 // but it is difficult to know whether THIS worker is reading or writing e,
171 // especially since we may switch from writing to reading. This code relies
172 // on Rock::IoState::writeableAnchor_ being set when we locked for writing.
173 if (e.mem_obj && e.mem_obj->swapout.sio != NULL &&
174 dynamic_cast<IoState&>(*e.mem_obj->swapout.sio).writeableAnchor_) {
175 map->abortWriting(e.swap_filen);
176 e.swap_dirn = -1;
177 e.swap_filen = -1;
178 e.swap_status = SWAPOUT_NONE;
179 dynamic_cast<IoState&>(*e.mem_obj->swapout.sio).writeableAnchor_ = NULL;
180 Store::Root().transientsAbandon(e); // broadcasts after the change
181 } else {
182 map->closeForReading(e.swap_filen);
183 e.swap_dirn = -1;
184 e.swap_filen = -1;
185 e.swap_status = SWAPOUT_NONE;
186 }
187 }
188
189 uint64_t
190 Rock::SwapDir::currentSize() const
191 {
192 const uint64_t spaceSize = !freeSlots ?
193 maxSize() : (slotSize * freeSlots->size());
194 // everything that is not free is in use
195 return maxSize() - spaceSize;
196 }
197
198 uint64_t
199 Rock::SwapDir::currentCount() const
200 {
201 return map ? map->entryCount() : 0;
202 }
203
204 /// In SMP mode only the disker process reports stats to avoid
205 /// counting the same stats by multiple processes.
206 bool
207 Rock::SwapDir::doReportStat() const
208 {
209 return ::SwapDir::doReportStat() && (!UsingSmp() || IamDiskProcess());
210 }
211
212 void
213 Rock::SwapDir::swappedOut(const StoreEntry &)
214 {
215 // stats are not stored but computed when needed
216 }
217
218 int64_t
219 Rock::SwapDir::slotLimitAbsolute() const
220 {
221 // the max value is an invalid one; all values must be below the limit
222 assert(std::numeric_limits<Ipc::StoreMapSliceId>::max() ==
223 std::numeric_limits<SlotId>::max());
224 return std::numeric_limits<SlotId>::max();
225 }
226
227 int64_t
228 Rock::SwapDir::slotLimitActual() const
229 {
230 const int64_t sWanted = (maxSize() - HeaderSize)/slotSize;
231 const int64_t sLimitLo = map ? map->sliceLimit() : 0; // dynamic shrinking unsupported
232 const int64_t sLimitHi = slotLimitAbsolute();
233 return min(max(sLimitLo, sWanted), sLimitHi);
234 }
235
236 int64_t
237 Rock::SwapDir::entryLimitActual() const
238 {
239 return min(slotLimitActual(), entryLimitAbsolute());
240 }
241
242 // TODO: encapsulate as a tool
243 void
244 Rock::SwapDir::create()
245 {
246 assert(path);
247 assert(filePath);
248
249 if (UsingSmp() && !IamDiskProcess()) {
250 debugs (47,3, HERE << "disker will create in " << path);
251 return;
252 }
253
254 debugs (47,3, HERE << "creating in " << path);
255
256 struct stat dir_sb;
257 if (::stat(path, &dir_sb) == 0) {
258 struct stat file_sb;
259 if (::stat(filePath, &file_sb) == 0) {
260 debugs (47, DBG_IMPORTANT, "Skipping existing Rock db: " << filePath);
261 return;
262 }
263 // else the db file is not there or is not accessible, and we will try
264 // to create it later below, generating a detailed error on failures.
265 } else { // path does not exist or is inaccessible
266 // If path exists but is not accessible, mkdir() below will fail, and
267 // the admin should see the error and act accordingly, so there is
268 // no need to distinguish ENOENT from other possible stat() errors.
269 debugs (47, DBG_IMPORTANT, "Creating Rock db directory: " << path);
270 const int res = mkdir(path, 0700);
271 if (res != 0)
272 createError("mkdir");
273 }
274
275 debugs (47, DBG_IMPORTANT, "Creating Rock db: " << filePath);
276 const int swap = open(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
277 if (swap < 0)
278 createError("create");
279
280 #if SLOWLY_FILL_WITH_ZEROS
281 char block[1024];
282 Must(maxSize() % sizeof(block) == 0);
283 memset(block, '\0', sizeof(block));
284
285 for (off_t offset = 0; offset < maxSize(); offset += sizeof(block)) {
286 if (write(swap, block, sizeof(block)) != sizeof(block))
287 createError("write");
288 }
289 #else
290 if (ftruncate(swap, maxSize()) != 0)
291 createError("truncate");
292
293 char header[HeaderSize];
294 memset(header, '\0', sizeof(header));
295 if (write(swap, header, sizeof(header)) != sizeof(header))
296 createError("write");
297 #endif
298
299 close(swap);
300 }
301
302 // report Rock DB creation error and exit
303 void
304 Rock::SwapDir::createError(const char *const msg)
305 {
306 debugs(47, DBG_CRITICAL, "ERROR: Failed to initialize Rock Store db in " <<
307 filePath << "; " << msg << " error: " << xstrerror());
308 fatal("Rock Store db creation error");
309 }
310
311 void
312 Rock::SwapDir::init()
313 {
314 debugs(47,2, HERE);
315
316 // XXX: SwapDirs aren't refcounted. We make IORequestor calls, which
317 // are refcounted. We up our count once to avoid implicit delete's.
318 lock();
319
320 freeSlots = shm_old(Ipc::Mem::PageStack)(freeSlotsPath());
321
322 Must(!map);
323 map = new DirMap(inodeMapPath());
324 map->cleaner = this;
325
326 const char *ioModule = needsDiskStrand() ? "IpcIo" : "Blocking";
327 if (DiskIOModule *m = DiskIOModule::Find(ioModule)) {
328 debugs(47,2, HERE << "Using DiskIO module: " << ioModule);
329 io = m->createStrategy();
330 io->init();
331 } else {
332 debugs(47, DBG_CRITICAL, "FATAL: Rock store is missing DiskIO module: " <<
333 ioModule);
334 fatal("Rock Store missing a required DiskIO module");
335 }
336
337 theFile = io->newFile(filePath);
338 theFile->configure(fileConfig);
339 theFile->open(O_RDWR, 0644, this);
340
341 // Increment early. Otherwise, if one SwapDir finishes rebuild before
342 // others start, storeRebuildComplete() will think the rebuild is over!
343 // TODO: move store_dirs_rebuilding hack to store modules that need it.
344 ++StoreController::store_dirs_rebuilding;
345 }
346
347 bool
348 Rock::SwapDir::needsDiskStrand() const
349 {
350 const bool wontEvenWorkWithoutDisker = Config.workers > 1;
351 const bool wouldWorkBetterWithDisker = DiskIOModule::Find("IpcIo");
352 return InDaemonMode() && (wontEvenWorkWithoutDisker ||
353 wouldWorkBetterWithDisker);
354 }
355
356 void
357 Rock::SwapDir::parse(int anIndex, char *aPath)
358 {
359 index = anIndex;
360
361 path = xstrdup(aPath);
362
363 // cache store is located at path/db
364 String fname(path);
365 fname.append("/rock");
366 filePath = xstrdup(fname.termedBuf());
367
368 parseSize(false);
369 parseOptions(0);
370
371 // Current openForWriting() code overwrites the old slot if needed
372 // and possible, so proactively removing old slots is probably useless.
373 assert(!repl); // repl = createRemovalPolicy(Config.replPolicy);
374
375 validateOptions();
376 }
377
378 void
379 Rock::SwapDir::reconfigure()
380 {
381 parseSize(true);
382 parseOptions(1);
383 // TODO: can we reconfigure the replacement policy (repl)?
384 validateOptions();
385 }
386
387 /// parse maximum db disk size
388 void
389 Rock::SwapDir::parseSize(const bool reconfig)
390 {
391 const int i = GetInteger();
392 if (i < 0)
393 fatal("negative Rock cache_dir size value");
394 const uint64_t new_max_size =
395 static_cast<uint64_t>(i) << 20; // MBytes to Bytes
396 if (!reconfig)
397 max_size = new_max_size;
398 else if (new_max_size != max_size) {
399 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir '" << path << "' size "
400 "cannot be changed dynamically, value left unchanged (" <<
401 (max_size >> 20) << " MB)");
402 }
403 }
404
405 ConfigOption *
406 Rock::SwapDir::getOptionTree() const
407 {
408 ConfigOptionVector *vector = dynamic_cast<ConfigOptionVector*>(::SwapDir::getOptionTree());
409 assert(vector);
410 vector->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::parseSizeOption, &SwapDir::dumpSizeOption));
411 vector->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::parseTimeOption, &SwapDir::dumpTimeOption));
412 vector->options.push_back(new ConfigOptionAdapter<SwapDir>(*const_cast<SwapDir *>(this), &SwapDir::parseRateOption, &SwapDir::dumpRateOption));
413 return vector;
414 }
415
416 bool
417 Rock::SwapDir::allowOptionReconfigure(const char *const option) const
418 {
419 return strcmp(option, "slot-size") != 0 &&
420 ::SwapDir::allowOptionReconfigure(option);
421 }
422
423 /// parses time-specific options; mimics ::SwapDir::optionObjectSizeParse()
424 bool
425 Rock::SwapDir::parseTimeOption(char const *option, const char *value, int reconfig)
426 {
427 // TODO: ::SwapDir or, better, Config should provide time-parsing routines,
428 // including time unit handling. Same for size and rate.
429
430 time_msec_t *storedTime;
431 if (strcmp(option, "swap-timeout") == 0)
432 storedTime = &fileConfig.ioTimeout;
433 else
434 return false;
435
436 if (!value)
437 self_destruct();
438
439 // TODO: handle time units and detect parsing errors better
440 const int64_t parsedValue = strtoll(value, NULL, 10);
441 if (parsedValue < 0) {
442 debugs(3, DBG_CRITICAL, "FATAL: cache_dir " << path << ' ' << option << " must not be negative but is: " << parsedValue);
443 self_destruct();
444 }
445
446 const time_msec_t newTime = static_cast<time_msec_t>(parsedValue);
447
448 if (!reconfig)
449 *storedTime = newTime;
450 else if (*storedTime != newTime) {
451 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir " << path << ' ' << option
452 << " cannot be changed dynamically, value left unchanged: " <<
453 *storedTime);
454 }
455
456 return true;
457 }
458
459 /// reports time-specific options; mimics ::SwapDir::optionObjectSizeDump()
460 void
461 Rock::SwapDir::dumpTimeOption(StoreEntry * e) const
462 {
463 if (fileConfig.ioTimeout)
464 storeAppendPrintf(e, " swap-timeout=%" PRId64,
465 static_cast<int64_t>(fileConfig.ioTimeout));
466 }
467
468 /// parses rate-specific options; mimics ::SwapDir::optionObjectSizeParse()
469 bool
470 Rock::SwapDir::parseRateOption(char const *option, const char *value, int isaReconfig)
471 {
472 int *storedRate;
473 if (strcmp(option, "max-swap-rate") == 0)
474 storedRate = &fileConfig.ioRate;
475 else
476 return false;
477
478 if (!value)
479 self_destruct();
480
481 // TODO: handle time units and detect parsing errors better
482 const int64_t parsedValue = strtoll(value, NULL, 10);
483 if (parsedValue < 0) {
484 debugs(3, DBG_CRITICAL, "FATAL: cache_dir " << path << ' ' << option << " must not be negative but is: " << parsedValue);
485 self_destruct();
486 }
487
488 const int newRate = static_cast<int>(parsedValue);
489
490 if (newRate < 0) {
491 debugs(3, DBG_CRITICAL, "FATAL: cache_dir " << path << ' ' << option << " must not be negative but is: " << newRate);
492 self_destruct();
493 }
494
495 if (!isaReconfig)
496 *storedRate = newRate;
497 else if (*storedRate != newRate) {
498 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir " << path << ' ' << option
499 << " cannot be changed dynamically, value left unchanged: " <<
500 *storedRate);
501 }
502
503 return true;
504 }
505
506 /// reports rate-specific options; mimics ::SwapDir::optionObjectSizeDump()
507 void
508 Rock::SwapDir::dumpRateOption(StoreEntry * e) const
509 {
510 if (fileConfig.ioRate >= 0)
511 storeAppendPrintf(e, " max-swap-rate=%d", fileConfig.ioRate);
512 }
513
514 /// parses size-specific options; mimics ::SwapDir::optionObjectSizeParse()
515 bool
516 Rock::SwapDir::parseSizeOption(char const *option, const char *value, int reconfig)
517 {
518 uint64_t *storedSize;
519 if (strcmp(option, "slot-size") == 0)
520 storedSize = &slotSize;
521 else
522 return false;
523
524 if (!value)
525 self_destruct();
526
527 // TODO: handle size units and detect parsing errors better
528 const uint64_t newSize = strtoll(value, NULL, 10);
529 if (newSize <= 0) {
530 debugs(3, DBG_CRITICAL, "FATAL: cache_dir " << path << ' ' << option << " must be positive; got: " << newSize);
531 self_destruct();
532 }
533
534 if (newSize <= sizeof(DbCellHeader)) {
535 debugs(3, DBG_CRITICAL, "FATAL: cache_dir " << path << ' ' << option << " must exceed " << sizeof(DbCellHeader) << "; got: " << newSize);
536 self_destruct();
537 }
538
539 if (!reconfig)
540 *storedSize = newSize;
541 else if (*storedSize != newSize) {
542 debugs(3, DBG_IMPORTANT, "WARNING: cache_dir " << path << ' ' << option
543 << " cannot be changed dynamically, value left unchanged: " <<
544 *storedSize);
545 }
546
547 return true;
548 }
549
550 /// reports size-specific options; mimics ::SwapDir::optionObjectSizeDump()
551 void
552 Rock::SwapDir::dumpSizeOption(StoreEntry * e) const
553 {
554 storeAppendPrintf(e, " slot-size=%" PRId64, slotSize);
555 }
556
557 /// check the results of the configuration; only level-0 debugging works here
558 void
559 Rock::SwapDir::validateOptions()
560 {
561 if (slotSize <= 0)
562 fatal("Rock store requires a positive slot-size");
563
564 const int64_t maxSizeRoundingWaste = 1024 * 1024; // size is configured in MB
565 const int64_t slotSizeRoundingWaste = slotSize;
566 const int64_t maxRoundingWaste =
567 max(maxSizeRoundingWaste, slotSizeRoundingWaste);
568
569 // an entry consumes at least one slot; round up to reduce false warnings
570 const int64_t blockSize = static_cast<int64_t>(slotSize);
571 const int64_t maxObjSize = max(blockSize,
572 ((maxObjectSize()+blockSize-1)/blockSize)*blockSize);
573
574 // Does the "sfileno*max-size" limit match configured db capacity?
575 const double entriesMayOccupy = entryLimitAbsolute()*static_cast<double>(maxObjSize);
576 if (entriesMayOccupy + maxRoundingWaste < maxSize()) {
577 const int64_t diskWasteSize = maxSize() - static_cast<int64_t>(entriesMayOccupy);
578 debugs(47, DBG_CRITICAL, "WARNING: Rock cache_dir " << path << " wastes disk space due to entry limits:" <<
579 "\n\tconfigured db capacity: " << maxSize() << " bytes" <<
580 "\n\tconfigured db slot size: " << slotSize << " bytes" <<
581 "\n\tconfigured maximum entry size: " << maxObjectSize() << " bytes" <<
582 "\n\tmaximum number of cache_dir entries supported by Squid: " << entryLimitAbsolute() <<
583 "\n\tdisk space all entries may use: " << entriesMayOccupy << " bytes" <<
584 "\n\tdisk space wasted: " << diskWasteSize << " bytes");
585 }
586
587 // Does the "absolute slot count" limit match configured db capacity?
588 const double slotsMayOccupy = slotLimitAbsolute()*static_cast<double>(slotSize);
589 if (slotsMayOccupy + maxRoundingWaste < maxSize()) {
590 const int64_t diskWasteSize = maxSize() - static_cast<int64_t>(entriesMayOccupy);
591 debugs(47, DBG_CRITICAL, "WARNING: Rock cache_dir " << path << " wastes disk space due to slot limits:" <<
592 "\n\tconfigured db capacity: " << maxSize() << " bytes" <<
593 "\n\tconfigured db slot size: " << slotSize << " bytes" <<
594 "\n\tmaximum number of rock cache_dir slots supported by Squid: " << slotLimitAbsolute() <<
595 "\n\tdisk space all slots may use: " << slotsMayOccupy << " bytes" <<
596 "\n\tdisk space wasted: " << diskWasteSize << " bytes");
597 }
598 }
599
600 void
601 Rock::SwapDir::rebuild()
602 {
603 //++StoreController::store_dirs_rebuilding; // see Rock::SwapDir::init()
604 AsyncJob::Start(new Rebuild(this));
605 }
606
607 bool
608 Rock::SwapDir::canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const
609 {
610 if (!::SwapDir::canStore(e, sizeof(DbCellHeader)+diskSpaceNeeded, load))
611 return false;
612
613 if (!theFile || !theFile->canWrite())
614 return false;
615
616 if (!map)
617 return false;
618
619 // Do not start I/O transaction if there are less than 10% free pages left.
620 // TODO: reserve page instead
621 if (needsDiskStrand() &&
622 Ipc::Mem::PageLevel(Ipc::Mem::PageId::ioPage) >= 0.9 * Ipc::Mem::PageLimit(Ipc::Mem::PageId::ioPage)) {
623 debugs(47, 5, HERE << "too few shared pages for IPC I/O left");
624 return false;
625 }
626
627 if (io->shedLoad())
628 return false;
629
630 load = io->load();
631 return true;
632 }
633
634 StoreIOState::Pointer
635 Rock::SwapDir::createStoreIO(StoreEntry &e, StoreIOState::STFNCB *cbFile, StoreIOState::STIOCB *cbIo, void *data)
636 {
637 if (!theFile || theFile->error()) {
638 debugs(47,4, HERE << theFile);
639 return NULL;
640 }
641
642 sfileno filen;
643 Ipc::StoreMapAnchor *const slot =
644 map->openForWriting(reinterpret_cast<const cache_key *>(e.key), filen);
645 if (!slot) {
646 debugs(47, 5, HERE << "map->add failed");
647 return NULL;
648 }
649
650 assert(filen >= 0);
651 slot->set(e);
652
653 // XXX: We rely on our caller, storeSwapOutStart(), to set e.fileno.
654 // If that does not happen, the entry will not decrement the read level!
655
656 Rock::SwapDir::Pointer self(this);
657 IoState *sio = new IoState(self, &e, cbFile, cbIo, data);
658
659 sio->swap_dirn = index;
660 sio->swap_filen = filen;
661 sio->writeableAnchor_ = slot;
662
663 debugs(47,5, HERE << "dir " << index << " created new filen " <<
664 std::setfill('0') << std::hex << std::uppercase << std::setw(8) <<
665 sio->swap_filen << std::dec << " starting at " <<
666 diskOffset(sio->swap_filen));
667
668 sio->file(theFile);
669
670 trackReferences(e);
671 return sio;
672 }
673
674 int64_t
675 Rock::SwapDir::diskOffset(const SlotId sid) const
676 {
677 assert(sid >= 0);
678 return HeaderSize + slotSize*sid;
679 }
680
681 int64_t
682 Rock::SwapDir::diskOffset(Ipc::Mem::PageId &pageId) const
683 {
684 assert(pageId);
685 return diskOffset(pageId.number - 1);
686 }
687
688 int64_t
689 Rock::SwapDir::diskOffsetLimit() const
690 {
691 assert(map);
692 return diskOffset(map->sliceLimit());
693 }
694
695 bool
696 Rock::SwapDir::useFreeSlot(Ipc::Mem::PageId &pageId)
697 {
698 if (freeSlots->pop(pageId)) {
699 debugs(47, 5, "got a previously free slot: " << pageId);
700 return true;
701 }
702
703 // catch free slots delivered to noteFreeMapSlice()
704 assert(!waitingForPage);
705 waitingForPage = &pageId;
706 if (map->purgeOne()) {
707 assert(!waitingForPage); // noteFreeMapSlice() should have cleared it
708 assert(pageId.set());
709 debugs(47, 5, "got a previously busy slot: " << pageId);
710 return true;
711 }
712 assert(waitingForPage == &pageId);
713 waitingForPage = NULL;
714
715 debugs(47, 3, "cannot get a slot; entries: " << map->entryCount());
716 return false;
717 }
718
719 bool
720 Rock::SwapDir::validSlotId(const SlotId slotId) const
721 {
722 return 0 <= slotId && slotId < slotLimitActual();
723 }
724
725 void
726 Rock::SwapDir::noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId)
727 {
728 Ipc::Mem::PageId pageId;
729 pageId.pool = index+1;
730 pageId.number = sliceId+1;
731 if (waitingForPage) {
732 *waitingForPage = pageId;
733 waitingForPage = NULL;
734 } else {
735 freeSlots->push(pageId);
736 }
737 }
738
739 // tries to open an old entry with swap_filen for reading
740 StoreIOState::Pointer
741 Rock::SwapDir::openStoreIO(StoreEntry &e, StoreIOState::STFNCB *cbFile, StoreIOState::STIOCB *cbIo, void *data)
742 {
743 if (!theFile || theFile->error()) {
744 debugs(47,4, HERE << theFile);
745 return NULL;
746 }
747
748 if (e.swap_filen < 0) {
749 debugs(47,4, HERE << e);
750 return NULL;
751 }
752
753 // Do not start I/O transaction if there are less than 10% free pages left.
754 // TODO: reserve page instead
755 if (needsDiskStrand() &&
756 Ipc::Mem::PageLevel(Ipc::Mem::PageId::ioPage) >= 0.9 * Ipc::Mem::PageLimit(Ipc::Mem::PageId::ioPage)) {
757 debugs(47, 5, HERE << "too few shared pages for IPC I/O left");
758 return NULL;
759 }
760
761 // The are two ways an entry can get swap_filen: our get() locked it for
762 // reading or our storeSwapOutStart() locked it for writing. Peeking at our
763 // locked entry is safe, but no support for reading the entry we swap out.
764 const Ipc::StoreMapAnchor *slot = map->peekAtReader(e.swap_filen);
765 if (!slot)
766 return NULL; // we were writing afterall
767
768 Rock::SwapDir::Pointer self(this);
769 IoState *sio = new IoState(self, &e, cbFile, cbIo, data);
770
771 sio->swap_dirn = index;
772 sio->swap_filen = e.swap_filen;
773 sio->readableAnchor_ = slot;
774 sio->file(theFile);
775
776 debugs(47,5, HERE << "dir " << index << " has old filen: " <<
777 std::setfill('0') << std::hex << std::uppercase << std::setw(8) <<
778 sio->swap_filen);
779
780 assert(slot->sameKey(static_cast<const cache_key*>(e.key)));
781 // For collapsed disk hits: e.swap_file_sz and slot->basics.swap_file_sz
782 // may still be zero and basics.swap_file_sz may grow.
783 assert(slot->basics.swap_file_sz >= e.swap_file_sz);
784
785 return sio;
786 }
787
788 void
789 Rock::SwapDir::ioCompletedNotification()
790 {
791 if (!theFile)
792 fatalf("Rock cache_dir failed to initialize db file: %s", filePath);
793
794 if (theFile->error())
795 fatalf("Rock cache_dir at %s failed to open db file: %s", filePath,
796 xstrerror());
797
798 debugs(47, 2, "Rock cache_dir[" << index << "] limits: " <<
799 std::setw(12) << maxSize() << " disk bytes, " <<
800 std::setw(7) << map->entryLimit() << " entries, and " <<
801 std::setw(7) << map->sliceLimit() << " slots");
802
803 rebuild();
804 }
805
806 void
807 Rock::SwapDir::closeCompleted()
808 {
809 theFile = NULL;
810 }
811
812 void
813 Rock::SwapDir::readCompleted(const char *, int rlen, int errflag, RefCount< ::ReadRequest> r)
814 {
815 ReadRequest *request = dynamic_cast<Rock::ReadRequest*>(r.getRaw());
816 assert(request);
817 IoState::Pointer sio = request->sio;
818
819 if (errflag == DISK_OK && rlen > 0)
820 sio->offset_ += rlen;
821
822 sio->callReaderBack(r->buf, rlen);
823 }
824
825 void
826 Rock::SwapDir::writeCompleted(int errflag, size_t, RefCount< ::WriteRequest> r)
827 {
828 Rock::WriteRequest *request = dynamic_cast<Rock::WriteRequest*>(r.getRaw());
829 assert(request);
830 assert(request->sio != NULL);
831 IoState &sio = *request->sio;
832
833 // quit if somebody called IoState::close() while we were waiting
834 if (!sio.stillWaiting()) {
835 debugs(79, 3, "ignoring closed entry " << sio.swap_filen);
836 noteFreeMapSlice(request->sidNext);
837 return;
838 }
839
840 // TODO: Fail if disk dropped one of the previous write requests.
841
842 if (errflag == DISK_OK) {
843 // do not increment sio.offset_ because we do it in sio->write()
844
845 // finalize the shared slice info after writing slice contents to disk
846 Ipc::StoreMap::Slice &slice =
847 map->writeableSlice(sio.swap_filen, request->sidCurrent);
848 slice.size = request->len - sizeof(DbCellHeader);
849 slice.next = request->sidNext;
850
851 if (request->eof) {
852 assert(sio.e);
853 assert(sio.writeableAnchor_);
854 sio.e->swap_file_sz = sio.writeableAnchor_->basics.swap_file_sz =
855 sio.offset_;
856
857 // close, the entry gets the read lock
858 map->closeForWriting(sio.swap_filen, true);
859 sio.writeableAnchor_ = NULL;
860 sio.finishedWriting(errflag);
861 }
862 } else {
863 noteFreeMapSlice(request->sidNext);
864
865 writeError(*sio.e);
866 sio.finishedWriting(errflag);
867 // and hope that Core will call disconnect() to close the map entry
868 }
869
870 CollapsedForwarding::Broadcast(*sio.e);
871 }
872
873 void
874 Rock::SwapDir::writeError(StoreEntry &e)
875 {
876 // Do not abortWriting here. The entry should keep the write lock
877 // instead of losing association with the store and confusing core.
878 map->freeEntry(e.swap_filen); // will mark as unusable, just in case
879
880 Store::Root().transientsAbandon(e);
881
882 // All callers must also call IoState callback, to propagate the error.
883 }
884
885 bool
886 Rock::SwapDir::full() const
887 {
888 return freeSlots != NULL && !freeSlots->size();
889 }
890
891 // storeSwapOutFileClosed calls this nethod on DISK_NO_SPACE_LEFT,
892 // but it should not happen for us
893 void
894 Rock::SwapDir::diskFull()
895 {
896 debugs(20, DBG_IMPORTANT, "BUG: No space left with rock cache_dir: " <<
897 filePath);
898 }
899
900 /// purge while full(); it should be sufficient to purge just one
901 void
902 Rock::SwapDir::maintain()
903 {
904 // The Store calls this to free some db space, but there is nothing wrong
905 // with a full() db, except when db has to shrink after reconfigure, and
906 // we do not support shrinking yet (it would have to purge specific slots).
907 // TODO: Disable maintain() requests when they are pointless.
908 }
909
910 void
911 Rock::SwapDir::reference(StoreEntry &e)
912 {
913 debugs(47, 5, HERE << &e << ' ' << e.swap_dirn << ' ' << e.swap_filen);
914 if (repl && repl->Referenced)
915 repl->Referenced(repl, &e, &e.repl);
916 }
917
918 bool
919 Rock::SwapDir::dereference(StoreEntry &e, bool)
920 {
921 debugs(47, 5, HERE << &e << ' ' << e.swap_dirn << ' ' << e.swap_filen);
922 if (repl && repl->Dereferenced)
923 repl->Dereferenced(repl, &e, &e.repl);
924
925 // no need to keep e in the global store_table for us; we have our own map
926 return false;
927 }
928
929 bool
930 Rock::SwapDir::unlinkdUseful() const
931 {
932 // no entry-specific files to unlink
933 return false;
934 }
935
936 void
937 Rock::SwapDir::unlink(StoreEntry &e)
938 {
939 debugs(47, 5, HERE << e);
940 ignoreReferences(e);
941 map->freeEntry(e.swap_filen);
942 disconnect(e);
943 }
944
945 void
946 Rock::SwapDir::markForUnlink(StoreEntry &e)
947 {
948 debugs(47, 5, e);
949 map->freeEntry(e.swap_filen);
950 }
951
952 void
953 Rock::SwapDir::trackReferences(StoreEntry &e)
954 {
955 debugs(47, 5, HERE << e);
956 if (repl)
957 repl->Add(repl, &e, &e.repl);
958 }
959
960 void
961 Rock::SwapDir::ignoreReferences(StoreEntry &e)
962 {
963 debugs(47, 5, HERE << e);
964 if (repl)
965 repl->Remove(repl, &e, &e.repl);
966 }
967
968 void
969 Rock::SwapDir::statfs(StoreEntry &e) const
970 {
971 storeAppendPrintf(&e, "\n");
972 storeAppendPrintf(&e, "Maximum Size: %" PRIu64 " KB\n", maxSize() >> 10);
973 storeAppendPrintf(&e, "Current Size: %.2f KB %.2f%%\n",
974 currentSize() / 1024.0,
975 Math::doublePercent(currentSize(), maxSize()));
976
977 const int entryLimit = entryLimitActual();
978 const int slotLimit = slotLimitActual();
979 storeAppendPrintf(&e, "Maximum entries: %9d\n", entryLimit);
980 if (map && entryLimit > 0) {
981 const int entryCount = map->entryCount();
982 storeAppendPrintf(&e, "Current entries: %9d %.2f%%\n",
983 entryCount, (100.0 * entryCount / entryLimit));
984 }
985
986 storeAppendPrintf(&e, "Maximum slots: %9d\n", slotLimit);
987 if (map && slotLimit > 0) {
988 const unsigned int slotsFree = !freeSlots ? 0 : freeSlots->size();
989 if (slotsFree <= static_cast<const unsigned int>(slotLimit)) {
990 const int usedSlots = slotLimit - static_cast<const int>(slotsFree);
991 storeAppendPrintf(&e, "Used slots: %9d %.2f%%\n",
992 usedSlots, (100.0 * usedSlots / slotLimit));
993 }
994 if (slotLimit < 100) { // XXX: otherwise too expensive to count
995 Ipc::ReadWriteLockStats stats;
996 map->updateStats(stats);
997 stats.dump(e);
998 }
999 }
1000
1001 storeAppendPrintf(&e, "Pending operations: %d out of %d\n",
1002 store_open_disk_fd, Config.max_open_disk_fds);
1003
1004 storeAppendPrintf(&e, "Flags:");
1005
1006 if (flags.selected)
1007 storeAppendPrintf(&e, " SELECTED");
1008
1009 if (flags.read_only)
1010 storeAppendPrintf(&e, " READ-ONLY");
1011
1012 storeAppendPrintf(&e, "\n");
1013
1014 }
1015
1016 SBuf
1017 Rock::SwapDir::inodeMapPath() const
1018 {
1019 return Ipc::Mem::Segment::Name(SBuf(path), "map");
1020 }
1021
1022 const char *
1023 Rock::SwapDir::freeSlotsPath() const
1024 {
1025 static String spacesPath;
1026 spacesPath = path;
1027 spacesPath.append("_spaces");
1028 return spacesPath.termedBuf();
1029 }
1030
1031 namespace Rock
1032 {
1033 RunnerRegistrationEntry(SwapDirRr);
1034 }
1035
1036 void Rock::SwapDirRr::create()
1037 {
1038 Must(mapOwners.empty() && freeSlotsOwners.empty());
1039 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
1040 if (const Rock::SwapDir *const sd = dynamic_cast<Rock::SwapDir *>(INDEXSD(i))) {
1041 const int64_t capacity = sd->slotLimitActual();
1042
1043 SwapDir::DirMap::Owner *const mapOwner =
1044 SwapDir::DirMap::Init(sd->inodeMapPath(), capacity);
1045 mapOwners.push_back(mapOwner);
1046
1047 // TODO: somehow remove pool id and counters from PageStack?
1048 Ipc::Mem::Owner<Ipc::Mem::PageStack> *const freeSlotsOwner =
1049 shm_new(Ipc::Mem::PageStack)(sd->freeSlotsPath(),
1050 i+1, capacity, 0);
1051 freeSlotsOwners.push_back(freeSlotsOwner);
1052
1053 // TODO: add method to initialize PageStack with no free pages
1054 while (true) {
1055 Ipc::Mem::PageId pageId;
1056 if (!freeSlotsOwner->object()->pop(pageId))
1057 break;
1058 }
1059 }
1060 }
1061 }
1062
1063 Rock::SwapDirRr::~SwapDirRr()
1064 {
1065 for (size_t i = 0; i < mapOwners.size(); ++i) {
1066 delete mapOwners[i];
1067 delete freeSlotsOwners[i];
1068 }
1069 }
1070