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