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