2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 47 Store Directory Routines */
11 #define CLEAN_BUF_SZ 16384
15 #include "ConfigOption.h"
16 #include "DiskIO/DiskIOModule.h"
17 #include "DiskIO/DiskIOStrategy.h"
23 #include "RebuildState.h"
24 #include "SquidConfig.h"
25 #include "SquidMath.h"
26 #include "SquidTime.h"
27 #include "StatCounters.h"
28 #include "store_key_md5.h"
29 #include "StoreSearchUFS.h"
30 #include "StoreSwapLogData.h"
32 #include "UFSSwapDir.h"
41 int Fs::Ufs::UFSSwapDir::NumberOfUFSDirs
= 0;
42 int *Fs::Ufs::UFSSwapDir::UFSDirToGlobalDirMapping
= NULL
;
44 class UFSCleanLog
: public SwapDir::CleanLog
48 UFSCleanLog(SwapDir
*);
49 /** Get the next entry that is a candidate for clean log writing
51 virtual const StoreEntry
*nextEntry();
52 /** "write" an entry to the clean log file.
54 virtual void write(StoreEntry
const &);
61 RemovalPolicyWalker
*walker
;
65 UFSCleanLog::UFSCleanLog(SwapDir
*aSwapDir
) :
66 cur(NULL
), newLog(NULL
), cln(NULL
), outbuf(NULL
),
67 outbuf_offset(0), fd(-1),walker(NULL
), sd(aSwapDir
)
71 UFSCleanLog::nextEntry()
73 const StoreEntry
*entry
= NULL
;
76 entry
= walker
->Next(walker
);
82 UFSCleanLog::write(StoreEntry
const &e
)
85 static size_t ss
= sizeof(StoreSwapLogData
);
86 s
.op
= (char) SWAP_LOG_ADD
;
87 s
.swap_filen
= e
.swap_filen
;
88 s
.timestamp
= e
.timestamp
;
89 s
.lastref
= e
.lastref
;
90 s
.expires
= e
.expires
;
91 s
.lastmod
= e
.lastmod
;
92 s
.swap_file_sz
= e
.swap_file_sz
;
93 s
.refcount
= e
.refcount
;
95 memcpy(&s
.key
, e
.key
, SQUID_MD5_DIGEST_LENGTH
);
97 memcpy(outbuf
+ outbuf_offset
, &s
, ss
);
101 if (outbuf_offset
+ ss
>= CLEAN_BUF_SZ
) {
102 if (FD_WRITE_METHOD(fd
, outbuf
, outbuf_offset
) < 0) {
103 /* XXX This error handling should probably move up to the caller */
104 debugs(50, DBG_CRITICAL
, HERE
<< newLog
<< ": write: " << xstrerror());
105 debugs(50, DBG_CRITICAL
, HERE
<< "Current swap logfile not replaced.");
119 Fs::Ufs::UFSSwapDir::canStore(const StoreEntry
&e
, int64_t diskSpaceNeeded
, int &load
) const
121 if (!SwapDir::canStore(e
, diskSpaceNeeded
, load
))
132 FreeObject(void *address
)
134 StoreSwapLogData
*anObject
= static_cast <StoreSwapLogData
*>(address
);
139 rev_int_sort(const void *A
, const void *B
)
141 const int *i1
= (const int *)A
;
142 const int *i2
= (const int *)B
;
147 Fs::Ufs::UFSSwapDir::parseSizeL1L2()
149 int i
= GetInteger();
151 fatal("UFSSwapDir::parseSizeL1L2: invalid size value");
153 const uint64_t size
= static_cast<uint64_t>(i
) << 20; // MBytes to Bytes
155 /* just reconfigure it */
157 if (size
== maxSize())
158 debugs(3, 2, "Cache dir '" << path
<< "' size remains unchanged at " << i
<< " MB");
160 debugs(3, DBG_IMPORTANT
, "Cache dir '" << path
<< "' size changed to " << i
<< " MB");
168 fatal("UFSSwapDir::parseSizeL1L2: invalid level 1 directories value");
173 fatal("UFSSwapDir::parseSizeL1L2: invalid level 2 directories value");
177 Fs::Ufs::UFSSwapDir::reconfigure()
184 Fs::Ufs::UFSSwapDir::parse (int anIndex
, char *aPath
)
187 path
= xstrdup(aPath
);
191 /* Initialise replacement policy stuff */
192 repl
= createRemovalPolicy(Config
.replPolicy
);
198 Fs::Ufs::UFSSwapDir::changeIO(DiskIOModule
*module
)
200 DiskIOStrategy
*anIO
= module
->createStrategy();
202 ioType
= xstrdup(module
->type());
206 /* Change the IO Options */
208 if (currentIOOptions
&& currentIOOptions
->options
.size() > 2) {
209 delete currentIOOptions
->options
.back();
210 currentIOOptions
->options
.pop_back();
213 /* TODO: factor out these 4 lines */
214 ConfigOption
*ioOptions
= IO
->io
->getOptionTree();
216 if (currentIOOptions
&& ioOptions
)
217 currentIOOptions
->options
.push_back(ioOptions
);
221 Fs::Ufs::UFSSwapDir::optionIOParse(char const *option
, const char *value
, int isaReconfig
)
223 if (strcmp(option
, "IOEngine") != 0)
227 /* silently ignore this */
233 DiskIOModule
*module
= DiskIOModule::Find(value
);
244 Fs::Ufs::UFSSwapDir::optionIODump(StoreEntry
* e
) const
246 storeAppendPrintf(e
, " IOEngine=%s", ioType
);
250 Fs::Ufs::UFSSwapDir::getOptionTree() const
252 ConfigOption
*parentResult
= SwapDir::getOptionTree();
254 if (currentIOOptions
== NULL
)
255 currentIOOptions
= new ConfigOptionVector();
257 currentIOOptions
->options
.push_back(parentResult
);
259 currentIOOptions
->options
.push_back(new ConfigOptionAdapter
<UFSSwapDir
>(*const_cast<UFSSwapDir
*>(this), &UFSSwapDir::optionIOParse
, &UFSSwapDir::optionIODump
));
261 if (ConfigOption
*ioOptions
= IO
->io
->getOptionTree())
262 currentIOOptions
->options
.push_back(ioOptions
);
264 ConfigOption
* result
= currentIOOptions
;
266 currentIOOptions
= NULL
;
272 Fs::Ufs::UFSSwapDir::init()
274 debugs(47, 3, HERE
<< "Initialising UFS SwapDir engine.");
275 /* Parsing must be finished by now - force to NULL, don't delete */
276 currentIOOptions
= NULL
;
277 static int started_clean_event
= 0;
278 static const char *errmsg
=
279 "\tFailed to verify one of the swap directories, Check cache.log\n"
280 "\tfor details. Run 'squid -z' to create swap directories\n"
281 "\tif needed, or if running Squid for the first time.";
284 if (verifyCacheDirs())
291 if (!started_clean_event
) {
292 eventAdd("UFS storeDirClean", CleanEvent
, NULL
, 15.0, 1);
293 started_clean_event
= 1;
296 (void) fsBlockSize(path
, &fs
.blksize
);
300 Fs::Ufs::UFSSwapDir::create()
302 debugs(47, 3, "Creating swap space in " << path
);
303 createDirectory(path
, 0);
307 Fs::Ufs::UFSSwapDir::UFSSwapDir(char const *aType
, const char *anIOType
) :
316 currentIOOptions(new ConfigOptionVector()),
317 ioType(xstrdup(anIOType
)),
321 /* modulename is only set to disk modules that are built, by configure,
322 * so the Find call should never return NULL here.
324 IO
= new Fs::Ufs::UFSStrategy(DiskIOModule::Find(anIOType
)->createStrategy());
327 Fs::Ufs::UFSSwapDir::~UFSSwapDir()
329 if (swaplog_fd
> -1) {
330 file_close(swaplog_fd
);
336 delete currentIOOptions
;
340 Fs::Ufs::UFSSwapDir::dumpEntry(StoreEntry
&e
) const
342 debugs(47, DBG_CRITICAL
, HERE
<< "FILENO "<< std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << e
.swap_filen
);
343 debugs(47, DBG_CRITICAL
, HERE
<< "PATH " << fullPath(e
.swap_filen
, NULL
) );
348 Fs::Ufs::UFSSwapDir::doubleCheck(StoreEntry
& e
)
353 if (::stat(fullPath(e
.swap_filen
, NULL
), &sb
) < 0) {
354 debugs(47, DBG_CRITICAL
, HERE
<< "WARNING: Missing swap file");
359 if ((off_t
)e
.swap_file_sz
!= sb
.st_size
) {
360 debugs(47, DBG_CRITICAL
, HERE
<< "WARNING: Size Mismatch. Entry size: "
361 << e
.swap_file_sz
<< ", file size: " << sb
.st_size
);
370 Fs::Ufs::UFSSwapDir::statfs(StoreEntry
& sentry
) const
377 storeAppendPrintf(&sentry
, "First level subdirectories: %d\n", l1
);
378 storeAppendPrintf(&sentry
, "Second level subdirectories: %d\n", l2
);
379 storeAppendPrintf(&sentry
, "Maximum Size: %" PRIu64
" KB\n", maxSize() >> 10);
380 storeAppendPrintf(&sentry
, "Current Size: %.2f KB\n", currentSize() / 1024.0);
381 storeAppendPrintf(&sentry
, "Percent Used: %0.2f%%\n",
382 Math::doublePercent(currentSize(), maxSize()));
383 storeAppendPrintf(&sentry
, "Filemap bits in use: %d of %d (%d%%)\n",
384 map
->numFilesInMap(), map
->capacity(),
385 Math::intPercent(map
->numFilesInMap(), map
->capacity()));
386 x
= fsStats(path
, &totl_kb
, &free_kb
, &totl_in
, &free_in
);
389 storeAppendPrintf(&sentry
, "Filesystem Space in use: %d/%d KB (%d%%)\n",
392 Math::intPercent(totl_kb
- free_kb
, totl_kb
));
393 storeAppendPrintf(&sentry
, "Filesystem Inodes in use: %d/%d (%d%%)\n",
396 Math::intPercent(totl_in
- free_in
, totl_in
));
399 storeAppendPrintf(&sentry
, "Flags:");
402 storeAppendPrintf(&sentry
, " SELECTED");
405 storeAppendPrintf(&sentry
, " READ-ONLY");
407 storeAppendPrintf(&sentry
, "\n");
413 Fs::Ufs::UFSSwapDir::maintain()
415 /* TODO: possible options for improvement;
417 * Note that too much aggression here is not good. It means that disk
418 * controller is getting a long queue of removals to act on, along
419 * with its regular I/O queue, and that client traffic is 'paused'
420 * and growing the network I/O queue as well while the scan happens.
421 * Possibly bad knock-on effects as Squid catches up on all that.
423 * Bug 2448 may have been a sign of what can wrong. At the least it
424 * provides a test case for aggression effects in overflow conditions.
426 * - base removal limit on space saved, instead of count ?
428 * - base removal rate on a traffic speed counter ?
429 * as the purge took up more time out of the second it would grow to
430 * a graceful full pause
432 * - pass out a value to cause another event to be scheduled immediately
433 * instead of waiting a whole second more ?
434 * knock on; schedule less if all caches are under low-water
436 * - admin configurable removal rate or count ?
437 * the current numbers are arbitrary, config helps with experimental
438 * trials and future-proofing the install base.
439 * we also have this indirectly by shifting the relative positions
440 * of low-, high- water and the total capacity limit.
443 // minSize() is swap_low_watermark in bytes
444 const uint64_t lowWaterSz
= minSize();
446 if (currentSize() < lowWaterSz
) {
447 debugs(47, 5, "space still available in " << path
);
451 /* We can't delete objects while rebuilding swap */
452 /* XXX each store should start maintaining as it comes online. */
453 if (StoreController::store_dirs_rebuilding
) {
454 // suppress the warnings, except once each minute
455 static int64_t lastWarn
= 0;
457 if (lastWarn
+60 < squid_curtime
) {
458 lastWarn
= squid_curtime
;
459 warnLevel
= DBG_IMPORTANT
;
461 debugs(47, warnLevel
, StoreController::store_dirs_rebuilding
<< " cache_dir still rebuilding. Skip GC for " << path
);
465 // maxSize() is cache_dir total size in bytes
466 const uint64_t highWaterSz
= ((maxSize() * Config
.Swap
.highWaterMark
) / 100);
468 // f is percentage of 'gap' filled between low- and high-water.
469 // Used to reduced purge rate when between water markers, and
470 // to multiply it more agressively the further above high-water
471 // it reaches. But in a graceful linear growth curve.
473 if (highWaterSz
> lowWaterSz
) {
474 // might be equal. n/0 is bad.
475 f
= (double) (currentSize() - lowWaterSz
) / (highWaterSz
- lowWaterSz
);
478 // how deep to look for a single object that can be removed
479 int max_scan
= (int) (f
* 400.0 + 100.0);
481 // try to purge only this many objects this cycle.
482 int max_remove
= (int) (f
* 300.0 + 20.0);
485 * This is kinda cheap, but so we need this priority hack?
487 debugs(47, 3, "f=" << f
<< ", max_scan=" << max_scan
<< ", max_remove=" << max_remove
);
489 RemovalPurgeWalker
*walker
= repl
->PurgeInit(repl
, max_scan
);
492 // only purge while above low-water
493 while (currentSize() >= lowWaterSz
) {
495 // stop if we reached max removals for this cycle,
496 // Bug 2448 may be from this not clearing enough,
497 // but it predates the current algorithm so not sure
498 if (removed
>= max_remove
)
501 StoreEntry
*e
= walker
->Next(walker
);
503 // stop if all objects are locked / in-use,
504 // or the cache is empty
506 break; /* no more objects */
513 walker
->Done(walker
);
514 debugs(47, (removed
? 2 : 3), path
<<
515 " removed " << removed
<< "/" << max_remove
<< " f=" <<
516 std::setprecision(4) << f
<< " max_scan=" << max_scan
);
518 // what if cache is still over the high watermark ?
519 // Store::Maintain() schedules another purge in 1 second.
523 Fs::Ufs::UFSSwapDir::reference(StoreEntry
&e
)
525 debugs(47, 3, HERE
<< "referencing " << &e
<< " " <<
526 e
.swap_dirn
<< "/" << e
.swap_filen
);
528 if (repl
->Referenced
)
529 repl
->Referenced(repl
, &e
, &e
.repl
);
533 Fs::Ufs::UFSSwapDir::dereference(StoreEntry
& e
)
535 debugs(47, 3, HERE
<< "dereferencing " << &e
<< " " <<
536 e
.swap_dirn
<< "/" << e
.swap_filen
);
538 if (repl
->Dereferenced
)
539 repl
->Dereferenced(repl
, &e
, &e
.repl
);
541 return true; // keep e in the global store_table
544 StoreIOState::Pointer
545 Fs::Ufs::UFSSwapDir::createStoreIO(StoreEntry
&e
, StoreIOState::STFNCB
* file_callback
, StoreIOState::STIOCB
* aCallback
, void *callback_data
)
547 return IO
->create (this, &e
, file_callback
, aCallback
, callback_data
);
550 StoreIOState::Pointer
551 Fs::Ufs::UFSSwapDir::openStoreIO(StoreEntry
&e
, StoreIOState::STFNCB
* file_callback
, StoreIOState::STIOCB
* aCallback
, void *callback_data
)
553 return IO
->open (this, &e
, file_callback
, aCallback
, callback_data
);
557 Fs::Ufs::UFSSwapDir::mapBitTest(sfileno filn
)
559 return map
->testBit(filn
);
563 Fs::Ufs::UFSSwapDir::mapBitSet(sfileno filn
)
569 Fs::Ufs::UFSSwapDir::mapBitReset(sfileno filn
)
572 * We have to test the bit before calling clearBit as
573 * it doesn't do bounds checking and blindly assumes
574 * filn is a valid file number, but it might not be because
575 * the map is dynamic in size. Also clearing an already clear
576 * bit puts the map counter of-of-whack.
579 if (map
->testBit(filn
))
584 Fs::Ufs::UFSSwapDir::mapBitAllocate()
587 fn
= map
->allocate(suggest
);
594 Fs::Ufs::UFSSwapDir::swapSubDir(int subdirn
)const
596 LOCAL_ARRAY(char, fullfilename
, MAXPATHLEN
);
597 assert(0 <= subdirn
&& subdirn
< l1
);
598 snprintf(fullfilename
, MAXPATHLEN
, "%s/%02X", path
, subdirn
);
603 Fs::Ufs::UFSSwapDir::createDirectory(const char *aPath
, int should_exist
)
610 if (0 == ::stat(aPath
, &st
)) {
611 if (S_ISDIR(st
.st_mode
)) {
612 debugs(47, (should_exist
? 3 : DBG_IMPORTANT
), aPath
<< " exists");
614 fatalf("Swap directory %s is not a directory.", aPath
);
616 } else if (0 == mkdir(aPath
, 0755)) {
617 debugs(47, (should_exist
? DBG_IMPORTANT
: 3), aPath
<< " created");
620 fatalf("Failed to make swap directory %s: %s",
628 Fs::Ufs::UFSSwapDir::pathIsDirectory(const char *aPath
)const
633 if (::stat(aPath
, &sb
) < 0) {
634 debugs(47, DBG_CRITICAL
, "ERROR: " << aPath
<< ": " << xstrerror());
638 if (S_ISDIR(sb
.st_mode
) == 0) {
639 debugs(47, DBG_CRITICAL
, "WARNING: " << aPath
<< " is not a directory");
647 Fs::Ufs::UFSSwapDir::verifyCacheDirs()
649 if (!pathIsDirectory(path
))
652 for (int j
= 0; j
< l1
; ++j
) {
653 char const *aPath
= swapSubDir(j
);
655 if (!pathIsDirectory(aPath
))
663 Fs::Ufs::UFSSwapDir::createSwapSubDirs()
665 LOCAL_ARRAY(char, name
, MAXPATHLEN
);
667 for (int i
= 0; i
< l1
; ++i
) {
668 snprintf(name
, MAXPATHLEN
, "%s/%02X", path
, i
);
672 if (createDirectory(name
, 0))
677 debugs(47, DBG_IMPORTANT
, "Making directories in " << name
);
679 for (int k
= 0; k
< l2
; ++k
) {
680 snprintf(name
, MAXPATHLEN
, "%s/%02X/%02X", path
, i
, k
);
681 createDirectory(name
, should_exist
);
687 Fs::Ufs::UFSSwapDir::logFile(char const *ext
) const
689 LOCAL_ARRAY(char, lpath
, MAXPATHLEN
);
690 LOCAL_ARRAY(char, pathtmp
, MAXPATHLEN
);
691 LOCAL_ARRAY(char, digit
, 32);
694 if (Config
.Log
.swap
) {
695 xstrncpy(pathtmp
, path
, MAXPATHLEN
- 64);
698 while ((pathtmp2
= strchr(pathtmp2
, '/')) != NULL
)
701 while (strlen(pathtmp
) && pathtmp
[strlen(pathtmp
) - 1] == '.')
702 pathtmp
[strlen(pathtmp
) - 1] = '\0';
704 for (pathtmp2
= pathtmp
; *pathtmp2
== '.'; ++pathtmp2
);
705 snprintf(lpath
, MAXPATHLEN
- 64, Config
.Log
.swap
, pathtmp2
);
707 if (strncmp(lpath
, Config
.Log
.swap
, MAXPATHLEN
- 64) == 0) {
709 snprintf(digit
, 32, "%02d", index
);
710 strncat(lpath
, digit
, 3);
713 xstrncpy(lpath
, path
, MAXPATHLEN
- 64);
714 strcat(lpath
, "/swap.state");
718 strncat(lpath
, ext
, 16);
724 Fs::Ufs::UFSSwapDir::openLog()
728 swaplog_fd
= file_open(logPath
, O_WRONLY
| O_CREAT
| O_BINARY
);
730 if (swaplog_fd
< 0) {
731 debugs(50, DBG_IMPORTANT
, "ERROR opening swap log " << logPath
<< ": " << xstrerror());
732 fatal("UFSSwapDir::openLog: Failed to open swap log.");
735 debugs(50, 3, HERE
<< "Cache Dir #" << index
<< " log opened on FD " << swaplog_fd
);
737 if (0 == NumberOfUFSDirs
)
738 assert(NULL
== UFSDirToGlobalDirMapping
);
742 assert(NumberOfUFSDirs
<= Config
.cacheSwap
.n_configured
);
746 Fs::Ufs::UFSSwapDir::closeLog()
748 if (swaplog_fd
< 0) /* not open */
751 file_close(swaplog_fd
);
753 debugs(47, 3, "Cache Dir #" << index
<< " log closed on FD " << swaplog_fd
);
759 assert(NumberOfUFSDirs
>= 0);
761 if (0 == NumberOfUFSDirs
)
762 safe_free(UFSDirToGlobalDirMapping
);
766 Fs::Ufs::UFSSwapDir::validL1(int anInt
) const
772 Fs::Ufs::UFSSwapDir::validL2(int anInt
) const
778 Fs::Ufs::UFSSwapDir::addDiskRestore(const cache_key
* key
,
780 uint64_t swap_file_sz
,
789 StoreEntry
*e
= NULL
;
790 debugs(47, 5, HERE
<< storeKeyText(key
) <<
791 ", fileno="<< std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << file_number
);
792 /* if you call this you'd better be sure file_number is not
794 e
= new StoreEntry();
795 e
->store_status
= STORE_OK
;
796 e
->setMemStatus(NOT_IN_MEMORY
);
797 e
->swap_status
= SWAPOUT_DONE
;
798 e
->swap_filen
= file_number
;
799 e
->swap_dirn
= index
;
800 e
->swap_file_sz
= swap_file_sz
;
801 e
->lastref
= lastref
;
802 e
->timestamp
= timestamp
;
803 e
->expires
= expires
;
804 e
->lastmod
= lastmod
;
805 e
->refcount
= refcount
;
807 EBIT_CLR(e
->flags
, RELEASE_REQUEST
);
808 EBIT_CLR(e
->flags
, KEY_PRIVATE
);
809 e
->ping_status
= PING_NONE
;
810 EBIT_CLR(e
->flags
, ENTRY_VALIDATED
);
811 mapBitSet(e
->swap_filen
);
812 cur_size
+= fs
.blksize
* sizeInBlocks(e
->swap_file_sz
);
814 e
->hashInsert(key
); /* do it after we clear KEY_PRIVATE */
820 Fs::Ufs::UFSSwapDir::undoAddDiskRestore(StoreEntry
*e
)
822 debugs(47, 5, HERE
<< *e
);
823 replacementRemove(e
); // checks swap_dirn so do it before we invalidate it
824 // Do not unlink the file as it might be used by a subsequent entry.
825 mapBitReset(e
->swap_filen
);
828 cur_size
-= fs
.blksize
* sizeInBlocks(e
->swap_file_sz
);
833 Fs::Ufs::UFSSwapDir::rebuild()
835 ++StoreController::store_dirs_rebuilding
;
836 eventAdd("storeRebuild", Fs::Ufs::RebuildState::RebuildStep
, new Fs::Ufs::RebuildState(this), 0.0, 1);
840 Fs::Ufs::UFSSwapDir::closeTmpSwapLog()
842 char *swaplog_path
= xstrdup(logFile(NULL
)); // where the swaplog should be
843 char *tmp_path
= xstrdup(logFile(".new")); // the temporary file we have generated
845 file_close(swaplog_fd
);
847 if (xrename(tmp_path
, swaplog_path
) < 0) {
848 fatalf("Failed to rename log file %s to %s", tmp_path
, swaplog_path
);
851 fd
= file_open(swaplog_path
, O_WRONLY
| O_CREAT
| O_BINARY
);
854 debugs(50, DBG_IMPORTANT
, "ERROR: " << swaplog_path
<< ": " << xstrerror());
855 fatalf("Failed to open swap log %s", swaplog_path
);
861 debugs(47, 3, "Cache Dir #" << index
<< " log opened on FD " << fd
);
865 Fs::Ufs::UFSSwapDir::openTmpSwapLog(int *clean_flag
, int *zero_flag
)
867 char *swaplog_path
= xstrdup(logFile(NULL
));
868 char *clean_path
= xstrdup(logFile(".last-clean"));
869 char *new_path
= xstrdup(logFile(".new"));
873 struct stat clean_sb
;
877 if (::stat(swaplog_path
, &log_sb
) < 0) {
878 debugs(47, DBG_IMPORTANT
, "Cache Dir #" << index
<< ": No log file");
879 safe_free(swaplog_path
);
880 safe_free(clean_path
);
885 *zero_flag
= log_sb
.st_size
== 0 ? 1 : 0;
886 /* close the existing write-only FD */
889 file_close(swaplog_fd
);
891 /* open a write-only FD for the new log */
892 fd
= file_open(new_path
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
);
895 debugs(50, DBG_IMPORTANT
, "ERROR: while opening swap log" << new_path
<< ": " << xstrerror());
896 fatalf("Failed to open swap log %s", new_path
);
902 const StoreSwapLogHeader header
;
904 buf
.init(header
.record_size
, header
.record_size
);
905 buf
.append(reinterpret_cast<const char*>(&header
), sizeof(header
));
906 // Pad to keep in sync with UFSSwapDir::writeCleanStart().
907 memset(buf
.space(), 0, header
.gapSize());
908 buf
.appended(header
.gapSize());
909 file_write(swaplog_fd
, -1, buf
.content(), buf
.contentSize(),
910 NULL
, NULL
, buf
.freeFunc());
913 /* open a read-only stream of the old log */
914 fp
= fopen(swaplog_path
, "rb");
917 debugs(50, DBG_CRITICAL
, "ERROR: while opening " << swaplog_path
<< ": " << xstrerror());
918 fatalf("Failed to open swap log for reading %s", swaplog_path
);
921 memset(&clean_sb
, '\0', sizeof(struct stat
));
923 if (::stat(clean_path
, &clean_sb
) < 0)
925 else if (clean_sb
.st_mtime
< log_sb
.st_mtime
)
930 safeunlink(clean_path
, 1);
932 safe_free(swaplog_path
);
934 safe_free(clean_path
);
942 * Begin the process to write clean cache state. For AUFS this means
943 * opening some log files and allocating write buffers. Return 0 if
944 * we succeed, and assign the 'func' and 'data' return pointers.
947 Fs::Ufs::UFSSwapDir::writeCleanStart()
949 UFSCleanLog
*state
= new UFSCleanLog(this);
950 StoreSwapLogHeader header
;
957 state
->newLog
= xstrdup(logFile(".clean"));
958 state
->fd
= file_open(state
->newLog
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
);
961 xfree(state
->newLog
);
966 state
->cur
= xstrdup(logFile(NULL
));
967 state
->cln
= xstrdup(logFile(".last-clean"));
968 state
->outbuf
= (char *)xcalloc(CLEAN_BUF_SZ
, 1);
969 state
->outbuf_offset
= 0;
971 memcpy(state
->outbuf
, &header
, sizeof(StoreSwapLogHeader
));
972 // Leave a gap to keep in sync with UFSSwapDir::openTmpSwapLog().
973 memset(state
->outbuf
+ sizeof(StoreSwapLogHeader
), 0, header
.gapSize());
974 state
->outbuf_offset
+= header
.record_size
;
976 state
->walker
= repl
->WalkInit(repl
);
977 ::unlink(state
->cln
);
978 debugs(47, 3, HERE
<< "opened " << state
->newLog
<< ", FD " << state
->fd
);
981 if (::stat(state
->cur
, &sb
) == 0)
982 fchmod(state
->fd
, sb
.st_mode
);
991 Fs::Ufs::UFSSwapDir::writeCleanDone()
993 UFSCleanLog
*state
= (UFSCleanLog
*)cleanLog
;
1002 state
->walker
->Done(state
->walker
);
1004 if (FD_WRITE_METHOD(state
->fd
, state
->outbuf
, state
->outbuf_offset
) < 0) {
1005 debugs(50, DBG_CRITICAL
, HERE
<< state
->newLog
<< ": write: " << xstrerror());
1006 debugs(50, DBG_CRITICAL
, HERE
<< "Current swap logfile not replaced.");
1007 file_close(state
->fd
);
1009 ::unlink(state
->newLog
);
1012 safe_free(state
->outbuf
);
1014 * You can't rename open files on Microsoft "operating systems"
1015 * so we have to close before renaming.
1018 /* save the fd value for a later test */
1022 if (state
->fd
>= 0) {
1023 #if _SQUID_OS2_ || _SQUID_WINDOWS_
1024 file_close(state
->fd
);
1028 xrename(state
->newLog
, state
->cur
);
1031 /* touch a timestamp file if we're not still validating */
1032 if (StoreController::store_dirs_rebuilding
)
1037 file_close(file_open(state
->cln
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
));
1040 safe_free(state
->cur
);
1042 safe_free(state
->newLog
);
1044 safe_free(state
->cln
);
1047 file_close(state
->fd
);
1057 Fs::Ufs::UFSSwapDir::CleanEvent(void *)
1059 static int swap_index
= 0;
1064 * Assert that there are UFS cache_dirs configured, otherwise
1065 * we should never be called.
1067 assert(NumberOfUFSDirs
);
1069 if (NULL
== UFSDirToGlobalDirMapping
) {
1072 * Initialize the little array that translates UFS cache_dir
1073 * number into the Config.cacheSwap.swapDirs array index.
1075 UFSDirToGlobalDirMapping
= (int *)xcalloc(NumberOfUFSDirs
, sizeof(*UFSDirToGlobalDirMapping
));
1077 for (i
= 0, n
= 0; i
< Config
.cacheSwap
.n_configured
; ++i
) {
1078 /* This is bogus, the controller should just clean each instance once */
1079 sd
= dynamic_cast <SwapDir
*>(INDEXSD(i
));
1081 if (!UFSSwapDir::IsUFSDir(sd
))
1084 UFSSwapDir
*usd
= dynamic_cast<UFSSwapDir
*>(sd
);
1088 UFSDirToGlobalDirMapping
[n
] = i
;
1091 j
+= (usd
->l1
* usd
->l2
);
1094 assert(n
== NumberOfUFSDirs
);
1096 * Start the commonUfsDirClean() swap_index with a random
1097 * value. j equals the total number of UFS level 2
1100 std::mt19937
mt(static_cast<uint32_t>(getCurrentTime() & 0xFFFFFFFF));
1101 xuniform_int_distribution
<> dist(0, j
);
1102 swap_index
= dist(mt
);
1105 /* if the rebuild is finished, start cleaning directories. */
1106 if (0 == StoreController::store_dirs_rebuilding
) {
1107 n
= DirClean(swap_index
);
1111 eventAdd("storeDirClean", CleanEvent
, NULL
,
1112 15.0 * exp(-0.25 * n
), 1);
1116 Fs::Ufs::UFSSwapDir::IsUFSDir(SwapDir
* sd
)
1118 UFSSwapDir
*mySD
= dynamic_cast<UFSSwapDir
*>(sd
);
1119 return (mySD
!= 0) ;
1123 * XXX: this is broken - it assumes all cache dirs use the same
1124 * l1 and l2 scheme. -RBC 20021215. Partial fix is in place -
1125 * if not UFSSwapDir return 0;
1128 Fs::Ufs::UFSSwapDir::FilenoBelongsHere(int fn
, int F0
, int F1
, int F2
)
1133 assert(F0
< Config
.cacheSwap
.n_configured
);
1134 assert (UFSSwapDir::IsUFSDir (dynamic_cast<SwapDir
*>(INDEXSD(F0
))));
1135 UFSSwapDir
*sd
= dynamic_cast<UFSSwapDir
*>(INDEXSD(F0
));
1144 D1
= ((filn
/ L2
) / L2
) % L1
;
1149 D2
= (filn
/ L2
) % L2
;
1158 Fs::Ufs::UFSSwapDir::validFileno(sfileno filn
, int flag
) const
1164 * If flag is set it means out-of-range file number should
1165 * be considered invalid.
1168 if (filn
> map
->capacity())
1175 Fs::Ufs::UFSSwapDir::unlinkFile(sfileno f
)
1177 debugs(79, 3, HERE
<< "unlinking fileno " << std::setfill('0') <<
1178 std::hex
<< std::uppercase
<< std::setw(8) << f
<< " '" <<
1179 fullPath(f
,NULL
) << "'");
1180 /* commonUfsDirMapBitReset(this, f); */
1181 IO
->unlinkFile(fullPath(f
,NULL
));
1185 Fs::Ufs::UFSSwapDir::unlinkdUseful() const
1187 // unlinkd may be useful only in workers
1188 return IamWorkerProcess() && IO
->io
->unlinkdUseful();
1192 Fs::Ufs::UFSSwapDir::unlink(StoreEntry
& e
)
1194 debugs(79, 3, HERE
<< "dirno " << index
<< ", fileno "<<
1195 std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << e
.swap_filen
);
1196 if (e
.swap_status
== SWAPOUT_DONE
) {
1197 cur_size
-= fs
.blksize
* sizeInBlocks(e
.swap_file_sz
);
1200 replacementRemove(&e
);
1201 mapBitReset(e
.swap_filen
);
1202 UFSSwapDir::unlinkFile(e
.swap_filen
);
1205 e
.swap_status
= SWAPOUT_NONE
;
1209 Fs::Ufs::UFSSwapDir::replacementAdd(StoreEntry
* e
)
1211 debugs(47, 4, HERE
<< "added node " << e
<< " to dir " << index
);
1212 repl
->Add(repl
, e
, &e
->repl
);
1216 Fs::Ufs::UFSSwapDir::replacementRemove(StoreEntry
* e
)
1218 if (e
->swap_dirn
< 0)
1221 SwapDirPointer SD
= INDEXSD(e
->swap_dirn
);
1223 assert (dynamic_cast<UFSSwapDir
*>(SD
.getRaw()) == this);
1225 debugs(47, 4, HERE
<< "remove node " << e
<< " from dir " << index
);
1227 repl
->Remove(repl
, e
, &e
->repl
);
1231 Fs::Ufs::UFSSwapDir::dump(StoreEntry
& entry
) const
1233 storeAppendPrintf(&entry
, " %" PRIu64
" %d %d", maxSize() >> 20, l1
, l2
);
1234 dumpOptions(&entry
);
1238 Fs::Ufs::UFSSwapDir::fullPath(sfileno filn
, char *fullpath
) const
1240 LOCAL_ARRAY(char, fullfilename
, MAXPATHLEN
);
1245 fullpath
= fullfilename
;
1249 snprintf(fullpath
, MAXPATHLEN
, "%s/%02X/%02X/%08X",
1251 ((filn
/ L2
) / L2
) % L1
,
1259 Fs::Ufs::UFSSwapDir::callback()
1261 return IO
->callback();
1265 Fs::Ufs::UFSSwapDir::sync()
1271 Fs::Ufs::UFSSwapDir::swappedOut(const StoreEntry
&e
)
1273 cur_size
+= fs
.blksize
* sizeInBlocks(e
.swap_file_sz
);
1278 Fs::Ufs::UFSSwapDir::logEntry(const StoreEntry
& e
, int op
) const
1280 StoreSwapLogData
*s
= new StoreSwapLogData
;
1282 s
->swap_filen
= e
.swap_filen
;
1283 s
->timestamp
= e
.timestamp
;
1284 s
->lastref
= e
.lastref
;
1285 s
->expires
= e
.expires
;
1286 s
->lastmod
= e
.lastmod
;
1287 s
->swap_file_sz
= e
.swap_file_sz
;
1288 s
->refcount
= e
.refcount
;
1290 memcpy(s
->key
, e
.key
, SQUID_MD5_DIGEST_LENGTH
);
1292 file_write(swaplog_fd
,
1295 sizeof(StoreSwapLogData
),
1302 Fs::Ufs::UFSSwapDir::DirClean(int swap_index
)
1304 DIR *dir_pointer
= NULL
;
1306 LOCAL_ARRAY(char, p1
, MAXPATHLEN
+ 1);
1307 LOCAL_ARRAY(char, p2
, MAXPATHLEN
+ 1);
1311 int fn
; /* same as swapfileno, but with dirn bits set */
1317 N0
= NumberOfUFSDirs
;
1318 D0
= UFSDirToGlobalDirMapping
[swap_index
% N0
];
1319 SD
= dynamic_cast<UFSSwapDir
*>(INDEXSD(D0
));
1322 D1
= (swap_index
/ N0
) % N1
;
1324 D2
= ((swap_index
/ N0
) / N1
) % N2
;
1325 snprintf(p1
, MAXPATHLEN
, "%s/%02X/%02X",
1327 debugs(36, 3, HERE
<< "Cleaning directory " << p1
);
1328 dir_pointer
= opendir(p1
);
1330 if (dir_pointer
== NULL
) {
1331 if (errno
== ENOENT
) {
1332 debugs(36, DBG_CRITICAL
, HERE
<< "WARNING: Creating " << p1
);
1333 if (mkdir(p1
, 0777) == 0)
1337 debugs(50, DBG_CRITICAL
, HERE
<< p1
<< ": " << xstrerror());
1343 while ((de
= readdir(dir_pointer
)) != NULL
&& k
< 20) {
1344 if (sscanf(de
->d_name
, "%X", &swapfileno
) != 1)
1347 fn
= swapfileno
; /* XXX should remove this cruft ! */
1349 if (SD
->validFileno(fn
, 1))
1350 if (SD
->mapBitTest(fn
))
1351 if (UFSSwapDir::FilenoBelongsHere(fn
, D0
, D1
, D2
))
1354 files
[k
] = swapfileno
;
1358 closedir(dir_pointer
);
1363 qsort(files
, k
, sizeof(int), rev_int_sort
);
1368 for (n
= 0; n
< k
; ++n
) {
1369 debugs(36, 3, HERE
<< "Cleaning file "<< std::setfill('0') << std::hex
<< std::uppercase
<< std::setw(8) << files
[n
]);
1370 snprintf(p2
, MAXPATHLEN
+ 1, "%s/%08X", p1
, files
[n
]);
1372 ++statCounter
.swap
.files_cleaned
;
1375 debugs(36, 3, HERE
<< "Cleaned " << k
<< " unused files from " << p1
);