]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/ufs/UFSSwapDir.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / fs / ufs / UFSSwapDir.cc
1 /*
2 * DEBUG: section 47 Store Directory Routines
3 * AUTHOR: Robert Collins
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 */
31
32 #define CLEAN_BUF_SZ 16384
33
34 #include "squid.h"
35 #include "cache_cf.h"
36 #include "ConfigOption.h"
37 #include "disk.h"
38 #include "DiskIO/DiskIOModule.h"
39 #include "DiskIO/DiskIOStrategy.h"
40 #include "fde.h"
41 #include "FileMap.h"
42 #include "globals.h"
43 #include "Parsing.h"
44 #include "RebuildState.h"
45 #include "SquidConfig.h"
46 #include "SquidMath.h"
47 #include "SquidTime.h"
48 #include "StatCounters.h"
49 #include "store_key_md5.h"
50 #include "StoreSearchUFS.h"
51 #include "StoreSwapLogData.h"
52 #include "tools.h"
53 #include "UFSSwapDir.h"
54
55 #if HAVE_MATH_H
56 #include <math.h>
57 #endif
58 #if HAVE_SYS_STAT_H
59 #include <sys/stat.h>
60 #endif
61 #if HAVE_ERRNO_H
62 #include <errno.h>
63 #endif
64
65 int Fs::Ufs::UFSSwapDir::NumberOfUFSDirs = 0;
66 int *Fs::Ufs::UFSSwapDir::UFSDirToGlobalDirMapping = NULL;
67
68 class UFSCleanLog : public SwapDir::CleanLog
69 {
70
71 public:
72 UFSCleanLog(SwapDir *);
73 /** Get the next entry that is a candidate for clean log writing
74 */
75 virtual const StoreEntry *nextEntry();
76 /** "write" an entry to the clean log file.
77 */
78 virtual void write(StoreEntry const &);
79 char *cur;
80 char *newLog;
81 char *cln;
82 char *outbuf;
83 off_t outbuf_offset;
84 int fd;
85 RemovalPolicyWalker *walker;
86 SwapDir *sd;
87 };
88
89 UFSCleanLog::UFSCleanLog(SwapDir *aSwapDir) :
90 cur(NULL), newLog(NULL), cln(NULL), outbuf(NULL),
91 outbuf_offset(0), fd(-1),walker(NULL), sd(aSwapDir)
92 {}
93
94 const StoreEntry *
95 UFSCleanLog::nextEntry()
96 {
97 const StoreEntry *entry = NULL;
98
99 if (walker)
100 entry = walker->Next(walker);
101
102 return entry;
103 }
104
105 void
106 UFSCleanLog::write(StoreEntry const &e)
107 {
108 StoreSwapLogData s;
109 static size_t ss = sizeof(StoreSwapLogData);
110 s.op = (char) SWAP_LOG_ADD;
111 s.swap_filen = e.swap_filen;
112 s.timestamp = e.timestamp;
113 s.lastref = e.lastref;
114 s.expires = e.expires;
115 s.lastmod = e.lastmod;
116 s.swap_file_sz = e.swap_file_sz;
117 s.refcount = e.refcount;
118 s.flags = e.flags;
119 memcpy(&s.key, e.key, SQUID_MD5_DIGEST_LENGTH);
120 s.finalize();
121 memcpy(outbuf + outbuf_offset, &s, ss);
122 outbuf_offset += ss;
123 /* buffered write */
124
125 if (outbuf_offset + ss >= CLEAN_BUF_SZ) {
126 if (FD_WRITE_METHOD(fd, outbuf, outbuf_offset) < 0) {
127 /* XXX This error handling should probably move up to the caller */
128 debugs(50, DBG_CRITICAL, HERE << newLog << ": write: " << xstrerror());
129 debugs(50, DBG_CRITICAL, HERE << "Current swap logfile not replaced.");
130 file_close(fd);
131 fd = -1;
132 unlink(newLog);
133 sd->cleanLog = NULL;
134 delete this;
135 return;
136 }
137
138 outbuf_offset = 0;
139 }
140 }
141
142 bool
143 Fs::Ufs::UFSSwapDir::canStore(const StoreEntry &e, int64_t diskSpaceNeeded, int &load) const
144 {
145 if (!SwapDir::canStore(e, diskSpaceNeeded, load))
146 return false;
147
148 if (IO->shedLoad())
149 return false;
150
151 load = IO->load();
152 return true;
153 }
154
155 static void
156 FreeObject(void *address)
157 {
158 StoreSwapLogData *anObject = static_cast <StoreSwapLogData *>(address);
159 delete anObject;
160 }
161
162 static QS rev_int_sort;
163 static int
164 rev_int_sort(const void *A, const void *B)
165 {
166 const int *i1 = (const int *)A;
167 const int *i2 = (const int *)B;
168 return *i2 - *i1;
169 }
170
171 void
172 Fs::Ufs::UFSSwapDir::parseSizeL1L2()
173 {
174 int i = GetInteger();
175 if (i <= 0)
176 fatal("UFSSwapDir::parseSizeL1L2: invalid size value");
177
178 const uint64_t size = static_cast<uint64_t>(i) << 20; // MBytes to Bytes
179
180 /* just reconfigure it */
181 if (reconfiguring) {
182 if (size == maxSize())
183 debugs(3, 2, "Cache dir '" << path << "' size remains unchanged at " << i << " MB");
184 else
185 debugs(3, DBG_IMPORTANT, "Cache dir '" << path << "' size changed to " << i << " MB");
186 }
187
188 max_size = size;
189
190 l1 = GetInteger();
191
192 if (l1 <= 0)
193 fatal("UFSSwapDir::parseSizeL1L2: invalid level 1 directories value");
194
195 l2 = GetInteger();
196
197 if (l2 <= 0)
198 fatal("UFSSwapDir::parseSizeL1L2: invalid level 2 directories value");
199 }
200
201 void
202 Fs::Ufs::UFSSwapDir::reconfigure()
203 {
204 parseSizeL1L2();
205 parseOptions(1);
206 }
207
208 void
209 Fs::Ufs::UFSSwapDir::parse (int anIndex, char *aPath)
210 {
211 index = anIndex;
212 path = xstrdup(aPath);
213
214 parseSizeL1L2();
215
216 /* Initialise replacement policy stuff */
217 repl = createRemovalPolicy(Config.replPolicy);
218
219 parseOptions(0);
220 }
221
222 void
223 Fs::Ufs::UFSSwapDir::changeIO(DiskIOModule *module)
224 {
225 DiskIOStrategy *anIO = module->createStrategy();
226 safe_free(ioType);
227 ioType = xstrdup(module->type());
228
229 delete IO->io;
230 IO->io = anIO;
231 /* Change the IO Options */
232
233 if (currentIOOptions && currentIOOptions->options.size() > 2)
234 delete currentIOOptions->options.pop_back();
235
236 /* TODO: factor out these 4 lines */
237 ConfigOption *ioOptions = IO->io->getOptionTree();
238
239 if (currentIOOptions && ioOptions)
240 currentIOOptions->options.push_back(ioOptions);
241 }
242
243 bool
244 Fs::Ufs::UFSSwapDir::optionIOParse(char const *option, const char *value, int isaReconfig)
245 {
246 if (strcmp(option, "IOEngine") != 0)
247 return false;
248
249 if (isaReconfig)
250 /* silently ignore this */
251 return true;
252
253 if (!value)
254 self_destruct();
255
256 DiskIOModule *module = DiskIOModule::Find(value);
257
258 if (!module)
259 self_destruct();
260
261 changeIO(module);
262
263 return true;
264 }
265
266 void
267 Fs::Ufs::UFSSwapDir::optionIODump(StoreEntry * e) const
268 {
269 storeAppendPrintf(e, " IOEngine=%s", ioType);
270 }
271
272 ConfigOption *
273 Fs::Ufs::UFSSwapDir::getOptionTree() const
274 {
275 ConfigOption *parentResult = SwapDir::getOptionTree();
276
277 if (currentIOOptions == NULL)
278 currentIOOptions = new ConfigOptionVector();
279
280 currentIOOptions->options.push_back(parentResult);
281
282 currentIOOptions->options.push_back(new ConfigOptionAdapter<UFSSwapDir>(*const_cast<UFSSwapDir *>(this), &UFSSwapDir::optionIOParse, &UFSSwapDir::optionIODump));
283
284 if (ConfigOption *ioOptions = IO->io->getOptionTree())
285 currentIOOptions->options.push_back(ioOptions);
286
287 ConfigOption* result = currentIOOptions;
288
289 currentIOOptions = NULL;
290
291 return result;
292 }
293
294 void
295 Fs::Ufs::UFSSwapDir::init()
296 {
297 debugs(47, 3, HERE << "Initialising UFS SwapDir engine.");
298 /* Parsing must be finished by now - force to NULL, don't delete */
299 currentIOOptions = NULL;
300 static int started_clean_event = 0;
301 static const char *errmsg =
302 "\tFailed to verify one of the swap directories, Check cache.log\n"
303 "\tfor details. Run 'squid -z' to create swap directories\n"
304 "\tif needed, or if running Squid for the first time.";
305 IO->init();
306
307 if (verifyCacheDirs())
308 fatal(errmsg);
309
310 openLog();
311
312 rebuild();
313
314 if (!started_clean_event) {
315 eventAdd("UFS storeDirClean", CleanEvent, NULL, 15.0, 1);
316 started_clean_event = 1;
317 }
318
319 (void) storeDirGetBlkSize(path, &fs.blksize);
320 }
321
322 void
323 Fs::Ufs::UFSSwapDir::create()
324 {
325 debugs(47, 3, "Creating swap space in " << path);
326 createDirectory(path, 0);
327 createSwapSubDirs();
328 }
329
330 Fs::Ufs::UFSSwapDir::UFSSwapDir(char const *aType, const char *anIOType) : SwapDir(aType), IO(NULL), map(new FileMap()), suggest(0), swaplog_fd (-1), currentIOOptions(new ConfigOptionVector()), ioType(xstrdup(anIOType)), cur_size(0), n_disk_objects(0)
331 {
332 /* modulename is only set to disk modules that are built, by configure,
333 * so the Find call should never return NULL here.
334 */
335 IO = new Fs::Ufs::UFSStrategy(DiskIOModule::Find(anIOType)->createStrategy());
336 }
337
338 Fs::Ufs::UFSSwapDir::~UFSSwapDir()
339 {
340 if (swaplog_fd > -1) {
341 file_close(swaplog_fd);
342 swaplog_fd = -1;
343 }
344 safe_free(ioType);
345 delete map;
346 delete IO;
347 delete currentIOOptions;
348 }
349
350 void
351 Fs::Ufs::UFSSwapDir::dumpEntry(StoreEntry &e) const
352 {
353 debugs(47, DBG_CRITICAL, HERE << "FILENO "<< std::setfill('0') << std::hex << std::uppercase << std::setw(8) << e.swap_filen);
354 debugs(47, DBG_CRITICAL, HERE << "PATH " << fullPath(e.swap_filen, NULL) );
355 e.dump(0);
356 }
357
358 bool
359 Fs::Ufs::UFSSwapDir::doubleCheck(StoreEntry & e)
360 {
361
362 struct stat sb;
363
364 if (::stat(fullPath(e.swap_filen, NULL), &sb) < 0) {
365 debugs(47, DBG_CRITICAL, HERE << "WARNING: Missing swap file");
366 dumpEntry(e);
367 return true;
368 }
369
370 if ((off_t)e.swap_file_sz != sb.st_size) {
371 debugs(47, DBG_CRITICAL, HERE << "WARNING: Size Mismatch. Entry size: "
372 << e.swap_file_sz << ", file size: " << sb.st_size);
373 dumpEntry(e);
374 return true;
375 }
376
377 return false;
378 }
379
380 void
381 Fs::Ufs::UFSSwapDir::statfs(StoreEntry & sentry) const
382 {
383 int totl_kb = 0;
384 int free_kb = 0;
385 int totl_in = 0;
386 int free_in = 0;
387 int x;
388 storeAppendPrintf(&sentry, "First level subdirectories: %d\n", l1);
389 storeAppendPrintf(&sentry, "Second level subdirectories: %d\n", l2);
390 storeAppendPrintf(&sentry, "Maximum Size: %" PRIu64 " KB\n", maxSize() >> 10);
391 storeAppendPrintf(&sentry, "Current Size: %.2f KB\n", currentSize() / 1024.0);
392 storeAppendPrintf(&sentry, "Percent Used: %0.2f%%\n",
393 Math::doublePercent(currentSize(), maxSize()));
394 storeAppendPrintf(&sentry, "Filemap bits in use: %d of %d (%d%%)\n",
395 map->numFilesInMap(), map->capacity(),
396 Math::intPercent(map->numFilesInMap(), map->capacity()));
397 x = storeDirGetUFSStats(path, &totl_kb, &free_kb, &totl_in, &free_in);
398
399 if (0 == x) {
400 storeAppendPrintf(&sentry, "Filesystem Space in use: %d/%d KB (%d%%)\n",
401 totl_kb - free_kb,
402 totl_kb,
403 Math::intPercent(totl_kb - free_kb, totl_kb));
404 storeAppendPrintf(&sentry, "Filesystem Inodes in use: %d/%d (%d%%)\n",
405 totl_in - free_in,
406 totl_in,
407 Math::intPercent(totl_in - free_in, totl_in));
408 }
409
410 storeAppendPrintf(&sentry, "Flags:");
411
412 if (flags.selected)
413 storeAppendPrintf(&sentry, " SELECTED");
414
415 if (flags.read_only)
416 storeAppendPrintf(&sentry, " READ-ONLY");
417
418 storeAppendPrintf(&sentry, "\n");
419
420 IO->statfs(sentry);
421 }
422
423 void
424 Fs::Ufs::UFSSwapDir::maintain()
425 {
426 /* We can't delete objects while rebuilding swap */
427
428 /* XXX FIXME each store should start maintaining as it comes online. */
429
430 if (StoreController::store_dirs_rebuilding)
431 return;
432
433 StoreEntry *e = NULL;
434
435 int removed = 0;
436
437 RemovalPurgeWalker *walker;
438
439 double f = (double) (currentSize() - minSize()) / (maxSize() - minSize());
440
441 f = f < 0.0 ? 0.0 : f > 1.0 ? 1.0 : f;
442
443 int max_scan = (int) (f * 400.0 + 100.0);
444
445 int max_remove = (int) (f * 70.0 + 10.0);
446
447 /*
448 * This is kinda cheap, but so we need this priority hack?
449 */
450
451 debugs(47, 3, HERE << "f=" << f << ", max_scan=" << max_scan << ", max_remove=" << max_remove );
452
453 walker = repl->PurgeInit(repl, max_scan);
454
455 while (1) {
456 if (currentSize() < minSize())
457 break;
458
459 if (removed >= max_remove)
460 break;
461
462 e = walker->Next(walker);
463
464 if (!e)
465 break; /* no more objects */
466
467 ++removed;
468
469 e->release();
470 }
471
472 walker->Done(walker);
473 debugs(47, (removed ? 2 : 3), HERE << path <<
474 " removed " << removed << "/" << max_remove << " f=" <<
475 std::setprecision(4) << f << " max_scan=" << max_scan);
476 }
477
478 void
479 Fs::Ufs::UFSSwapDir::reference(StoreEntry &e)
480 {
481 debugs(47, 3, HERE << "referencing " << &e << " " <<
482 e.swap_dirn << "/" << e.swap_filen);
483
484 if (repl->Referenced)
485 repl->Referenced(repl, &e, &e.repl);
486 }
487
488 bool
489 Fs::Ufs::UFSSwapDir::dereference(StoreEntry & e, bool)
490 {
491 debugs(47, 3, HERE << "dereferencing " << &e << " " <<
492 e.swap_dirn << "/" << e.swap_filen);
493
494 if (repl->Dereferenced)
495 repl->Dereferenced(repl, &e, &e.repl);
496
497 return true; // keep e in the global store_table
498 }
499
500 StoreIOState::Pointer
501 Fs::Ufs::UFSSwapDir::createStoreIO(StoreEntry &e, StoreIOState::STFNCB * file_callback, StoreIOState::STIOCB * aCallback, void *callback_data)
502 {
503 return IO->create (this, &e, file_callback, aCallback, callback_data);
504 }
505
506 StoreIOState::Pointer
507 Fs::Ufs::UFSSwapDir::openStoreIO(StoreEntry &e, StoreIOState::STFNCB * file_callback, StoreIOState::STIOCB * aCallback, void *callback_data)
508 {
509 return IO->open (this, &e, file_callback, aCallback, callback_data);
510 }
511
512 int
513 Fs::Ufs::UFSSwapDir::mapBitTest(sfileno filn)
514 {
515 return map->testBit(filn);
516 }
517
518 void
519 Fs::Ufs::UFSSwapDir::mapBitSet(sfileno filn)
520 {
521 map->setBit(filn);
522 }
523
524 void
525 Fs::Ufs::UFSSwapDir::mapBitReset(sfileno filn)
526 {
527 /*
528 * We have to test the bit before calling clearBit as
529 * it doesn't do bounds checking and blindly assumes
530 * filn is a valid file number, but it might not be because
531 * the map is dynamic in size. Also clearing an already clear
532 * bit puts the map counter of-of-whack.
533 */
534
535 if (map->testBit(filn))
536 map->clearBit(filn);
537 }
538
539 int
540 Fs::Ufs::UFSSwapDir::mapBitAllocate()
541 {
542 int fn;
543 fn = map->allocate(suggest);
544 map->setBit(fn);
545 suggest = fn + 1;
546 return fn;
547 }
548
549 char *
550 Fs::Ufs::UFSSwapDir::swapSubDir(int subdirn)const
551 {
552 LOCAL_ARRAY(char, fullfilename, MAXPATHLEN);
553 assert(0 <= subdirn && subdirn < l1);
554 snprintf(fullfilename, MAXPATHLEN, "%s/%02X", path, subdirn);
555 return fullfilename;
556 }
557
558 int
559 Fs::Ufs::UFSSwapDir::createDirectory(const char *aPath, int should_exist)
560 {
561 int created = 0;
562
563 struct stat st;
564 getCurrentTime();
565
566 if (0 == ::stat(aPath, &st)) {
567 if (S_ISDIR(st.st_mode)) {
568 debugs(47, (should_exist ? 3 : DBG_IMPORTANT), aPath << " exists");
569 } else {
570 fatalf("Swap directory %s is not a directory.", aPath);
571 }
572 } else if (0 == mkdir(aPath, 0755)) {
573 debugs(47, (should_exist ? DBG_IMPORTANT : 3), aPath << " created");
574 created = 1;
575 } else {
576 fatalf("Failed to make swap directory %s: %s",
577 aPath, xstrerror());
578 }
579
580 return created;
581 }
582
583 bool
584 Fs::Ufs::UFSSwapDir::pathIsDirectory(const char *aPath)const
585 {
586
587 struct stat sb;
588
589 if (::stat(aPath, &sb) < 0) {
590 debugs(47, DBG_CRITICAL, "ERROR: " << aPath << ": " << xstrerror());
591 return false;
592 }
593
594 if (S_ISDIR(sb.st_mode) == 0) {
595 debugs(47, DBG_CRITICAL, "WARNING: " << aPath << " is not a directory");
596 return false;
597 }
598
599 return true;
600 }
601
602 bool
603 Fs::Ufs::UFSSwapDir::verifyCacheDirs()
604 {
605 if (!pathIsDirectory(path))
606 return true;
607
608 for (int j = 0; j < l1; ++j) {
609 char const *aPath = swapSubDir(j);
610
611 if (!pathIsDirectory(aPath))
612 return true;
613 }
614
615 return false;
616 }
617
618 void
619 Fs::Ufs::UFSSwapDir::createSwapSubDirs()
620 {
621 LOCAL_ARRAY(char, name, MAXPATHLEN);
622
623 for (int i = 0; i < l1; ++i) {
624 snprintf(name, MAXPATHLEN, "%s/%02X", path, i);
625
626 int should_exist;
627
628 if (createDirectory(name, 0))
629 should_exist = 0;
630 else
631 should_exist = 1;
632
633 debugs(47, DBG_IMPORTANT, "Making directories in " << name);
634
635 for (int k = 0; k < l2; ++k) {
636 snprintf(name, MAXPATHLEN, "%s/%02X/%02X", path, i, k);
637 createDirectory(name, should_exist);
638 }
639 }
640 }
641
642 char *
643 Fs::Ufs::UFSSwapDir::logFile(char const *ext) const
644 {
645 LOCAL_ARRAY(char, lpath, MAXPATHLEN);
646 LOCAL_ARRAY(char, pathtmp, MAXPATHLEN);
647 LOCAL_ARRAY(char, digit, 32);
648 char *pathtmp2;
649
650 if (Config.Log.swap) {
651 xstrncpy(pathtmp, path, MAXPATHLEN - 64);
652 pathtmp2 = pathtmp;
653
654 while ((pathtmp2 = strchr(pathtmp2, '/')) != NULL)
655 *pathtmp2 = '.';
656
657 while (strlen(pathtmp) && pathtmp[strlen(pathtmp) - 1] == '.')
658 pathtmp[strlen(pathtmp) - 1] = '\0';
659
660 for (pathtmp2 = pathtmp; *pathtmp2 == '.'; ++pathtmp2);
661 snprintf(lpath, MAXPATHLEN - 64, Config.Log.swap, pathtmp2);
662
663 if (strncmp(lpath, Config.Log.swap, MAXPATHLEN - 64) == 0) {
664 strcat(lpath, ".");
665 snprintf(digit, 32, "%02d", index);
666 strncat(lpath, digit, 3);
667 }
668 } else {
669 xstrncpy(lpath, path, MAXPATHLEN - 64);
670 strcat(lpath, "/swap.state");
671 }
672
673 if (ext)
674 strncat(lpath, ext, 16);
675
676 return lpath;
677 }
678
679 void
680 Fs::Ufs::UFSSwapDir::openLog()
681 {
682 char *logPath;
683 logPath = logFile();
684 swaplog_fd = file_open(logPath, O_WRONLY | O_CREAT | O_BINARY);
685
686 if (swaplog_fd < 0) {
687 debugs(50, DBG_IMPORTANT, "ERROR opening swap log " << logPath << ": " << xstrerror());
688 fatal("UFSSwapDir::openLog: Failed to open swap log.");
689 }
690
691 debugs(50, 3, HERE << "Cache Dir #" << index << " log opened on FD " << swaplog_fd);
692
693 if (0 == NumberOfUFSDirs)
694 assert(NULL == UFSDirToGlobalDirMapping);
695
696 ++NumberOfUFSDirs;
697
698 assert(NumberOfUFSDirs <= Config.cacheSwap.n_configured);
699 }
700
701 void
702 Fs::Ufs::UFSSwapDir::closeLog()
703 {
704 if (swaplog_fd < 0) /* not open */
705 return;
706
707 file_close(swaplog_fd);
708
709 debugs(47, 3, "Cache Dir #" << index << " log closed on FD " << swaplog_fd);
710
711 swaplog_fd = -1;
712
713 --NumberOfUFSDirs;
714
715 assert(NumberOfUFSDirs >= 0);
716
717 if (0 == NumberOfUFSDirs)
718 safe_free(UFSDirToGlobalDirMapping);
719 }
720
721 bool
722 Fs::Ufs::UFSSwapDir::validL1(int anInt) const
723 {
724 return anInt < l1;
725 }
726
727 bool
728 Fs::Ufs::UFSSwapDir::validL2(int anInt) const
729 {
730 return anInt < l2;
731 }
732
733 StoreEntry *
734 Fs::Ufs::UFSSwapDir::addDiskRestore(const cache_key * key,
735 sfileno file_number,
736 uint64_t swap_file_sz,
737 time_t expires,
738 time_t timestamp,
739 time_t lastref,
740 time_t lastmod,
741 uint32_t refcount,
742 uint16_t newFlags,
743 int clean)
744 {
745 StoreEntry *e = NULL;
746 debugs(47, 5, HERE << storeKeyText(key) <<
747 ", fileno="<< std::setfill('0') << std::hex << std::uppercase << std::setw(8) << file_number);
748 /* if you call this you'd better be sure file_number is not
749 * already in use! */
750 e = new StoreEntry();
751 e->store_status = STORE_OK;
752 e->setMemStatus(NOT_IN_MEMORY);
753 e->swap_status = SWAPOUT_DONE;
754 e->swap_filen = file_number;
755 e->swap_dirn = index;
756 e->swap_file_sz = swap_file_sz;
757 e->lock_count = 0;
758 e->lastref = lastref;
759 e->timestamp = timestamp;
760 e->expires = expires;
761 e->lastmod = lastmod;
762 e->refcount = refcount;
763 e->flags = newFlags;
764 EBIT_SET(e->flags, ENTRY_CACHABLE);
765 EBIT_CLR(e->flags, RELEASE_REQUEST);
766 EBIT_CLR(e->flags, KEY_PRIVATE);
767 e->ping_status = PING_NONE;
768 EBIT_CLR(e->flags, ENTRY_VALIDATED);
769 mapBitSet(e->swap_filen);
770 cur_size += fs.blksize * sizeInBlocks(e->swap_file_sz);
771 ++n_disk_objects;
772 e->hashInsert(key); /* do it after we clear KEY_PRIVATE */
773 replacementAdd (e);
774 return e;
775 }
776
777 void
778 Fs::Ufs::UFSSwapDir::undoAddDiskRestore(StoreEntry *e)
779 {
780 debugs(47, 5, HERE << *e);
781 replacementRemove(e); // checks swap_dirn so do it before we invalidate it
782 // Do not unlink the file as it might be used by a subsequent entry.
783 mapBitReset(e->swap_filen);
784 e->swap_filen = -1;
785 e->swap_dirn = -1;
786 cur_size -= fs.blksize * sizeInBlocks(e->swap_file_sz);
787 --n_disk_objects;
788 }
789
790 void
791 Fs::Ufs::UFSSwapDir::rebuild()
792 {
793 ++StoreController::store_dirs_rebuilding;
794 eventAdd("storeRebuild", Fs::Ufs::RebuildState::RebuildStep, new Fs::Ufs::RebuildState(this), 0.0, 1);
795 }
796
797 void
798 Fs::Ufs::UFSSwapDir::closeTmpSwapLog()
799 {
800 char *swaplog_path = xstrdup(logFile(NULL)); // where the swaplog should be
801 char *tmp_path = xstrdup(logFile(".new")); // the temporary file we have generated
802 int fd;
803 file_close(swaplog_fd);
804
805 if (xrename(tmp_path, swaplog_path) < 0) {
806 fatalf("Failed to rename log file %s to %s", tmp_path, swaplog_path);
807 }
808
809 fd = file_open(swaplog_path, O_WRONLY | O_CREAT | O_BINARY);
810
811 if (fd < 0) {
812 debugs(50, DBG_IMPORTANT, "ERROR: " << swaplog_path << ": " << xstrerror());
813 fatalf("Failed to open swap log %s", swaplog_path);
814 }
815
816 xfree(swaplog_path);
817 xfree(tmp_path);
818 swaplog_fd = fd;
819 debugs(47, 3, "Cache Dir #" << index << " log opened on FD " << fd);
820 }
821
822 FILE *
823 Fs::Ufs::UFSSwapDir::openTmpSwapLog(int *clean_flag, int *zero_flag)
824 {
825 char *swaplog_path = xstrdup(logFile(NULL));
826 char *clean_path = xstrdup(logFile(".last-clean"));
827 char *new_path = xstrdup(logFile(".new"));
828
829 struct stat log_sb;
830
831 struct stat clean_sb;
832 FILE *fp;
833 int fd;
834
835 if (::stat(swaplog_path, &log_sb) < 0) {
836 debugs(47, DBG_IMPORTANT, "Cache Dir #" << index << ": No log file");
837 safe_free(swaplog_path);
838 safe_free(clean_path);
839 safe_free(new_path);
840 return NULL;
841 }
842
843 *zero_flag = log_sb.st_size == 0 ? 1 : 0;
844 /* close the existing write-only FD */
845
846 if (swaplog_fd >= 0)
847 file_close(swaplog_fd);
848
849 /* open a write-only FD for the new log */
850 fd = file_open(new_path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
851
852 if (fd < 0) {
853 debugs(50, DBG_IMPORTANT, "ERROR: while opening swap log" << new_path << ": " << xstrerror());
854 fatalf("Failed to open swap log %s", new_path);
855 }
856
857 swaplog_fd = fd;
858
859 {
860 const StoreSwapLogHeader header;
861 MemBuf buf;
862 buf.init(header.record_size, header.record_size);
863 buf.append(reinterpret_cast<const char*>(&header), sizeof(header));
864 // Pad to keep in sync with UFSSwapDir::writeCleanStart().
865 memset(buf.space(), 0, header.gapSize());
866 buf.appended(header.gapSize());
867 file_write(swaplog_fd, -1, buf.content(), buf.contentSize(),
868 NULL, NULL, buf.freeFunc());
869 }
870
871 /* open a read-only stream of the old log */
872 fp = fopen(swaplog_path, "rb");
873
874 if (fp == NULL) {
875 debugs(50, DBG_CRITICAL, "ERROR: while opening " << swaplog_path << ": " << xstrerror());
876 fatalf("Failed to open swap log for reading %s", swaplog_path);
877 }
878
879 memset(&clean_sb, '\0', sizeof(struct stat));
880
881 if (::stat(clean_path, &clean_sb) < 0)
882 *clean_flag = 0;
883 else if (clean_sb.st_mtime < log_sb.st_mtime)
884 *clean_flag = 0;
885 else
886 *clean_flag = 1;
887
888 safeunlink(clean_path, 1);
889
890 safe_free(swaplog_path);
891
892 safe_free(clean_path);
893
894 safe_free(new_path);
895
896 return fp;
897 }
898
899 /*
900 * Begin the process to write clean cache state. For AUFS this means
901 * opening some log files and allocating write buffers. Return 0 if
902 * we succeed, and assign the 'func' and 'data' return pointers.
903 */
904 int
905 Fs::Ufs::UFSSwapDir::writeCleanStart()
906 {
907 UFSCleanLog *state = new UFSCleanLog(this);
908 StoreSwapLogHeader header;
909 #if HAVE_FCHMOD
910
911 struct stat sb;
912 #endif
913
914 cleanLog = NULL;
915 state->newLog = xstrdup(logFile(".clean"));
916 state->fd = file_open(state->newLog, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
917
918 if (state->fd < 0) {
919 xfree(state->newLog);
920 delete state;
921 return -1;
922 }
923
924 state->cur = xstrdup(logFile(NULL));
925 state->cln = xstrdup(logFile(".last-clean"));
926 state->outbuf = (char *)xcalloc(CLEAN_BUF_SZ, 1);
927 state->outbuf_offset = 0;
928 /*copy the header */
929 memcpy(state->outbuf, &header, sizeof(StoreSwapLogHeader));
930 // Leave a gap to keep in sync with UFSSwapDir::openTmpSwapLog().
931 memset(state->outbuf + sizeof(StoreSwapLogHeader), 0, header.gapSize());
932 state->outbuf_offset += header.record_size;
933
934 state->walker = repl->WalkInit(repl);
935 ::unlink(state->cln);
936 debugs(47, 3, HERE << "opened " << state->newLog << ", FD " << state->fd);
937 #if HAVE_FCHMOD
938
939 if (::stat(state->cur, &sb) == 0)
940 fchmod(state->fd, sb.st_mode);
941
942 #endif
943
944 cleanLog = state;
945 return 0;
946 }
947
948 void
949 Fs::Ufs::UFSSwapDir::writeCleanDone()
950 {
951 UFSCleanLog *state = (UFSCleanLog *)cleanLog;
952 int fd;
953
954 if (NULL == state)
955 return;
956
957 if (state->fd < 0)
958 return;
959
960 state->walker->Done(state->walker);
961
962 if (FD_WRITE_METHOD(state->fd, state->outbuf, state->outbuf_offset) < 0) {
963 debugs(50, DBG_CRITICAL, HERE << state->newLog << ": write: " << xstrerror());
964 debugs(50, DBG_CRITICAL, HERE << "Current swap logfile not replaced.");
965 file_close(state->fd);
966 state->fd = -1;
967 ::unlink(state->newLog);
968 }
969
970 safe_free(state->outbuf);
971 /*
972 * You can't rename open files on Microsoft "operating systems"
973 * so we have to close before renaming.
974 */
975 closeLog();
976 /* save the fd value for a later test */
977 fd = state->fd;
978 /* rename */
979
980 if (state->fd >= 0) {
981 #if _SQUID_OS2_ || _SQUID_WINDOWS_
982 file_close(state->fd);
983 state->fd = -1;
984 #endif
985
986 xrename(state->newLog, state->cur);
987 }
988
989 /* touch a timestamp file if we're not still validating */
990 if (StoreController::store_dirs_rebuilding)
991 (void) 0;
992 else if (fd < 0)
993 (void) 0;
994 else
995 file_close(file_open(state->cln, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY));
996
997 /* close */
998 safe_free(state->cur);
999
1000 safe_free(state->newLog);
1001
1002 safe_free(state->cln);
1003
1004 if (state->fd >= 0)
1005 file_close(state->fd);
1006
1007 state->fd = -1;
1008
1009 delete state;
1010
1011 cleanLog = NULL;
1012 }
1013
1014 void
1015 Fs::Ufs::UFSSwapDir::CleanEvent(void *unused)
1016 {
1017 static int swap_index = 0;
1018 int i;
1019 int j = 0;
1020 int n = 0;
1021 /*
1022 * Assert that there are UFS cache_dirs configured, otherwise
1023 * we should never be called.
1024 */
1025 assert(NumberOfUFSDirs);
1026
1027 if (NULL == UFSDirToGlobalDirMapping) {
1028 SwapDir *sd;
1029 /*
1030 * Initialize the little array that translates UFS cache_dir
1031 * number into the Config.cacheSwap.swapDirs array index.
1032 */
1033 UFSDirToGlobalDirMapping = (int *)xcalloc(NumberOfUFSDirs, sizeof(*UFSDirToGlobalDirMapping));
1034
1035 for (i = 0, n = 0; i < Config.cacheSwap.n_configured; ++i) {
1036 /* This is bogus, the controller should just clean each instance once */
1037 sd = dynamic_cast <SwapDir *>(INDEXSD(i));
1038
1039 if (!UFSSwapDir::IsUFSDir(sd))
1040 continue;
1041
1042 UFSSwapDir *usd = dynamic_cast<UFSSwapDir *>(sd);
1043
1044 assert (usd);
1045
1046 UFSDirToGlobalDirMapping[n] = i;
1047 ++n;
1048
1049 j += (usd->l1 * usd->l2);
1050 }
1051
1052 assert(n == NumberOfUFSDirs);
1053 /*
1054 * Start the commonUfsDirClean() swap_index with a random
1055 * value. j equals the total number of UFS level 2
1056 * swap directories
1057 */
1058 swap_index = (int) (squid_random() % j);
1059 }
1060
1061 /* if the rebuild is finished, start cleaning directories. */
1062 if (0 == StoreController::store_dirs_rebuilding) {
1063 n = DirClean(swap_index);
1064 ++swap_index;
1065 }
1066
1067 eventAdd("storeDirClean", CleanEvent, NULL,
1068 15.0 * exp(-0.25 * n), 1);
1069 }
1070
1071 bool
1072 Fs::Ufs::UFSSwapDir::IsUFSDir(SwapDir * sd)
1073 {
1074 UFSSwapDir *mySD = dynamic_cast<UFSSwapDir *>(sd);
1075 return (mySD != 0) ;
1076 }
1077
1078 /*
1079 * XXX: this is broken - it assumes all cache dirs use the same
1080 * l1 and l2 scheme. -RBC 20021215. Partial fix is in place -
1081 * if not UFSSwapDir return 0;
1082 */
1083 bool
1084 Fs::Ufs::UFSSwapDir::FilenoBelongsHere(int fn, int F0, int F1, int F2)
1085 {
1086 int D1, D2;
1087 int L1, L2;
1088 int filn = fn;
1089 assert(F0 < Config.cacheSwap.n_configured);
1090 assert (UFSSwapDir::IsUFSDir (dynamic_cast<SwapDir *>(INDEXSD(F0))));
1091 UFSSwapDir *sd = dynamic_cast<UFSSwapDir *>(INDEXSD(F0));
1092
1093 if (!sd)
1094 return 0;
1095
1096 L1 = sd->l1;
1097
1098 L2 = sd->l2;
1099
1100 D1 = ((filn / L2) / L2) % L1;
1101
1102 if (F1 != D1)
1103 return 0;
1104
1105 D2 = (filn / L2) % L2;
1106
1107 if (F2 != D2)
1108 return 0;
1109
1110 return 1;
1111 }
1112
1113 int
1114 Fs::Ufs::UFSSwapDir::validFileno(sfileno filn, int flag) const
1115 {
1116 if (filn < 0)
1117 return 0;
1118
1119 /*
1120 * If flag is set it means out-of-range file number should
1121 * be considered invalid.
1122 */
1123 if (flag)
1124 if (filn > map->capacity())
1125 return 0;
1126
1127 return 1;
1128 }
1129
1130 void
1131 Fs::Ufs::UFSSwapDir::unlinkFile(sfileno f)
1132 {
1133 debugs(79, 3, HERE << "unlinking fileno " << std::setfill('0') <<
1134 std::hex << std::uppercase << std::setw(8) << f << " '" <<
1135 fullPath(f,NULL) << "'");
1136 /* commonUfsDirMapBitReset(this, f); */
1137 IO->unlinkFile(fullPath(f,NULL));
1138 }
1139
1140 bool
1141 Fs::Ufs::UFSSwapDir::unlinkdUseful() const
1142 {
1143 // unlinkd may be useful only in workers
1144 return IamWorkerProcess() && IO->io->unlinkdUseful();
1145 }
1146
1147 void
1148 Fs::Ufs::UFSSwapDir::unlink(StoreEntry & e)
1149 {
1150 debugs(79, 3, HERE << "dirno " << index << ", fileno "<<
1151 std::setfill('0') << std::hex << std::uppercase << std::setw(8) << e.swap_filen);
1152 if (e.swap_status == SWAPOUT_DONE) {
1153 cur_size -= fs.blksize * sizeInBlocks(e.swap_file_sz);
1154 --n_disk_objects;
1155 }
1156 replacementRemove(&e);
1157 mapBitReset(e.swap_filen);
1158 UFSSwapDir::unlinkFile(e.swap_filen);
1159 }
1160
1161 void
1162 Fs::Ufs::UFSSwapDir::replacementAdd(StoreEntry * e)
1163 {
1164 debugs(47, 4, HERE << "added node " << e << " to dir " << index);
1165 repl->Add(repl, e, &e->repl);
1166 }
1167
1168 void
1169 Fs::Ufs::UFSSwapDir::replacementRemove(StoreEntry * e)
1170 {
1171 StorePointer SD;
1172
1173 if (e->swap_dirn < 0)
1174 return;
1175
1176 SD = INDEXSD(e->swap_dirn);
1177
1178 assert (dynamic_cast<UFSSwapDir *>(SD.getRaw()) == this);
1179
1180 debugs(47, 4, HERE << "remove node " << e << " from dir " << index);
1181
1182 repl->Remove(repl, e, &e->repl);
1183 }
1184
1185 void
1186 Fs::Ufs::UFSSwapDir::dump(StoreEntry & entry) const
1187 {
1188 storeAppendPrintf(&entry, " %" PRIu64 " %d %d", maxSize() >> 20, l1, l2);
1189 dumpOptions(&entry);
1190 }
1191
1192 char *
1193 Fs::Ufs::UFSSwapDir::fullPath(sfileno filn, char *fullpath) const
1194 {
1195 LOCAL_ARRAY(char, fullfilename, MAXPATHLEN);
1196 int L1 = l1;
1197 int L2 = l2;
1198
1199 if (!fullpath)
1200 fullpath = fullfilename;
1201
1202 fullpath[0] = '\0';
1203
1204 snprintf(fullpath, MAXPATHLEN, "%s/%02X/%02X/%08X",
1205 path,
1206 ((filn / L2) / L2) % L1,
1207 (filn / L2) % L2,
1208 filn);
1209
1210 return fullpath;
1211 }
1212
1213 int
1214 Fs::Ufs::UFSSwapDir::callback()
1215 {
1216 return IO->callback();
1217 }
1218
1219 void
1220 Fs::Ufs::UFSSwapDir::sync()
1221 {
1222 IO->sync();
1223 }
1224
1225 void
1226 Fs::Ufs::UFSSwapDir::swappedOut(const StoreEntry &e)
1227 {
1228 cur_size += fs.blksize * sizeInBlocks(e.swap_file_sz);
1229 ++n_disk_objects;
1230 }
1231
1232 StoreSearch *
1233 Fs::Ufs::UFSSwapDir::search(String const url, HttpRequest *request)
1234 {
1235 if (url.size())
1236 fatal ("Cannot search by url yet\n");
1237
1238 return new Fs::Ufs::StoreSearchUFS (this);
1239 }
1240
1241 void
1242 Fs::Ufs::UFSSwapDir::logEntry(const StoreEntry & e, int op) const
1243 {
1244 StoreSwapLogData *s = new StoreSwapLogData;
1245 s->op = (char) op;
1246 s->swap_filen = e.swap_filen;
1247 s->timestamp = e.timestamp;
1248 s->lastref = e.lastref;
1249 s->expires = e.expires;
1250 s->lastmod = e.lastmod;
1251 s->swap_file_sz = e.swap_file_sz;
1252 s->refcount = e.refcount;
1253 s->flags = e.flags;
1254 memcpy(s->key, e.key, SQUID_MD5_DIGEST_LENGTH);
1255 s->finalize();
1256 file_write(swaplog_fd,
1257 -1,
1258 s,
1259 sizeof(StoreSwapLogData),
1260 NULL,
1261 NULL,
1262 FreeObject);
1263 }
1264
1265 int
1266 Fs::Ufs::UFSSwapDir::DirClean(int swap_index)
1267 {
1268 DIR *dir_pointer = NULL;
1269
1270 LOCAL_ARRAY(char, p1, MAXPATHLEN + 1);
1271 LOCAL_ARRAY(char, p2, MAXPATHLEN + 1);
1272
1273 int files[20];
1274 int swapfileno;
1275 int fn; /* same as swapfileno, but with dirn bits set */
1276 int n = 0;
1277 int k = 0;
1278 int N0, N1, N2;
1279 int D0, D1, D2;
1280 UFSSwapDir *SD;
1281 N0 = NumberOfUFSDirs;
1282 D0 = UFSDirToGlobalDirMapping[swap_index % N0];
1283 SD = dynamic_cast<UFSSwapDir *>(INDEXSD(D0));
1284 assert (SD);
1285 N1 = SD->l1;
1286 D1 = (swap_index / N0) % N1;
1287 N2 = SD->l2;
1288 D2 = ((swap_index / N0) / N1) % N2;
1289 snprintf(p1, MAXPATHLEN, "%s/%02X/%02X",
1290 SD->path, D1, D2);
1291 debugs(36, 3, HERE << "Cleaning directory " << p1);
1292 dir_pointer = opendir(p1);
1293
1294 if (dir_pointer == NULL) {
1295 if (errno == ENOENT) {
1296 debugs(36, DBG_CRITICAL, HERE << "WARNING: Creating " << p1);
1297 if (mkdir(p1, 0777) == 0)
1298 return 0;
1299 }
1300
1301 debugs(50, DBG_CRITICAL, HERE << p1 << ": " << xstrerror());
1302 safeunlink(p1, 1);
1303 return 0;
1304 }
1305
1306 dirent_t *de;
1307 while ((de = readdir(dir_pointer)) != NULL && k < 20) {
1308 if (sscanf(de->d_name, "%X", &swapfileno) != 1)
1309 continue;
1310
1311 fn = swapfileno; /* XXX should remove this cruft ! */
1312
1313 if (SD->validFileno(fn, 1))
1314 if (SD->mapBitTest(fn))
1315 if (UFSSwapDir::FilenoBelongsHere(fn, D0, D1, D2))
1316 continue;
1317
1318 files[k] = swapfileno;
1319 ++k;
1320 }
1321
1322 closedir(dir_pointer);
1323
1324 if (k == 0)
1325 return 0;
1326
1327 qsort(files, k, sizeof(int), rev_int_sort);
1328
1329 if (k > 10)
1330 k = 10;
1331
1332 for (n = 0; n < k; ++n) {
1333 debugs(36, 3, HERE << "Cleaning file "<< std::setfill('0') << std::hex << std::uppercase << std::setw(8) << files[n]);
1334 snprintf(p2, MAXPATHLEN + 1, "%s/%08X", p1, files[n]);
1335 safeunlink(p2, 0);
1336 ++statCounter.swap.files_cleaned;
1337 }
1338
1339 debugs(36, 3, HERE << "Cleaned " << k << " unused files from " << p1);
1340 return k;
1341 }