00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <sys/stat.h>
00025 #if defined(__digital__)
00026 #include <sys/mode.h>
00027 #endif
00028
00029
00030 #include <Basics.H>
00031 #include <FS.H>
00032 #include <Table.H>
00033 #include <Sequence.H>
00034 #include <VestaLog.H>
00035 #include <AtomicFile.H>
00036
00037
00038 #include <FP.H>
00039
00040
00041 #include <BitVector.H>
00042 #include <CacheConfig.H>
00043 #include <CacheState.H>
00044 #include <Debug.H>
00045 #include <PKPrefix.H>
00046
00047
00048 #include "CacheConfigServer.H"
00049 #include "EmptyPKLog.H"
00050 #include "SPKFileRep.H"
00051 #include "CacheEntry.H"
00052 #include "SPKFile.H"
00053 #include "SMultiPKFileRep.H"
00054 #include "VPKFile.H"
00055 #include "SMultiPKFile.H"
00056
00057 using std::ios;
00058 using std::streampos;
00059 using std::ifstream;
00060 using std::cerr;
00061 using std::cout;
00062 using std::endl;
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #define MISTRUST_PKLEN
00073
00074
00075
00076
00077
00078
00079 static const int ArcBits = 8;
00080
00081
00082
00083 void SMultiPKFile::OpenRead(const PKPrefix::T &pfx, ifstream &ifs)
00084 throw (SMultiPKFileRep::NotFound, FS::Failure)
00085 {
00086 try {
00087
00088 Text fpath(FullName(pfx));
00089 FS::OpenReadOnly(fpath, ifs);
00090 }
00091 catch (FS::DoesNotExist) {
00092 throw SMultiPKFileRep::NotFound();
00093 }
00094 }
00095
00096 streampos SMultiPKFile::SeekToPKFile(ifstream &ifs, const FP::Tag &pk,
00097 int &version) throw (SMultiPKFileRep::NotFound, FS::Failure)
00098 {
00099 try {
00100
00101 SMultiPKFileRep::Header hdr(ifs);
00102
00103
00104 streampos seek;
00105 switch (hdr.type) {
00106 case SMultiPKFileRep::HT_List:
00107 if (hdr.version <= SPKFileRep::SourceFuncVersion) {
00108 seek = SeekInListV1(ifs, hdr.num, pk);
00109 } else {
00110
00111 assert(false);
00112 }
00113 break;
00114 case SMultiPKFileRep::HT_SortedList:
00115 if (hdr.version <= SPKFileRep::SourceFuncVersion) {
00116 seek = SeekInSortedListV1(ifs, hdr.num, pk);
00117 } else {
00118
00119 assert(false);
00120 }
00121 break;
00122 case SMultiPKFileRep::HT_HashTable:
00123
00124 assert(false);
00125 default:
00126 assert(false);
00127 }
00128 FS::Seek(ifs, seek);
00129 version = hdr.version;
00130 return seek;
00131 }
00132 catch (SMultiPKFileRep::BadMPKFile) {
00133
00134 cerr << "Fatal error: tried reading bad MultiPKFile "
00135 << "in SMultiPKFile::SeekToPKFile; exiting..." << endl;
00136 assert(false);
00137 }
00138 catch (FS::EndOfFile) {
00139
00140 cerr << "Fatal error: premature EOF in "
00141 << "SMultiPKFile::SeekToPKFile; exiting..." << endl;
00142 assert(false);
00143 }
00144 return 0;
00145 }
00146
00147 streampos SMultiPKFile::SeekInListV1(ifstream &ifs,
00148 SMultiPKFileRep::UShort numEntries, const FP::Tag &pk)
00149 throw (SMultiPKFileRep::NotFound, FS::EndOfFile, FS::Failure)
00150 {
00151 while (numEntries > 0) {
00152 SMultiPKFileRep::HeaderEntry ent(ifs);
00153 if (ent.pk == pk) return (streampos)(ent.offset);
00154 numEntries--;
00155 }
00156 throw SMultiPKFileRep::NotFound();
00157 }
00158
00159 streampos SMultiPKFile::SeekInSortedListV1(ifstream &ifs,
00160 SMultiPKFileRep::UShort numEntries, const FP::Tag &pk)
00161 throw (SMultiPKFileRep::NotFound, FS::EndOfFile, FS::Failure)
00162 {
00163
00164 int lo = 0, hi = numEntries;
00165 streampos base = FS::Posn(ifs);
00166 while (lo < hi) {
00167
00168
00169
00170
00171
00172 int mid = (lo + hi) / 2;
00173 assert(lo <= mid && mid < hi);
00174 FS::Seek(ifs, base +
00175 (streampos) (mid * SMultiPKFileRep::HeaderEntry::Size()));
00176 SMultiPKFileRep::HeaderEntry midEntry(ifs);
00177
00178
00179 int cmp = Compare(pk, midEntry.pk);
00180 if (cmp < 0) {
00181
00182 hi = mid;
00183 } else if (cmp > 0) {
00184
00185 lo = mid + 1;
00186 } else {
00187 return (streampos)(midEntry.offset);
00188 }
00189 }
00190 throw SMultiPKFileRep::NotFound();
00191 }
00192
00193
00194
00195 bool SMultiPKFile::ChkptForRewrite(VPKFileMap& vpkFiles,
00196 const BitVector *toDelete,
00197 ChkPtTbl &vpkChkptTbl)
00198 throw()
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 {
00216 FP::Tag pk;
00217 VPKFile *vpkfile;
00218 VPKFileIter it(&vpkFiles);
00219
00220
00221
00222
00223
00224 if ((toDelete == (BitVector *)NULL)) {
00225 bool anyNewEntries = false;
00226 while (!anyNewEntries && it.Next( pk, vpkfile)) {
00227 vpkfile->mu.lock();
00228 if (vpkfile->HasNewEntries()) { anyNewEntries = true; }
00229 vpkfile->mu.unlock();
00230 }
00231 if (!anyNewEntries) return false;
00232 }
00233
00234
00235 for (it.Reset(); it.Next( pk, vpkfile); ) {
00236 VPKFileChkPt *chkpt = vpkfile->CheckPoint();
00237 bool inTbl = vpkChkptTbl.Put(pk, chkpt, false);
00238 assert(!inTbl);
00239 }
00240 vpkChkptTbl.Resize();
00241
00242
00243
00244 return true;
00245 }
00246
00247 bool SMultiPKFile::PrepareForRewrite(const PKPrefix::T &pfx,
00248 ifstream &ifs,
00249 SMultiPKFileRep::Header *&hdr)
00250 throw (FS::Failure, FS::EndOfFile)
00251 {
00252 bool mpkFileExists;
00253
00254 try
00255 {
00256 OpenRead(pfx, ifs);
00257 mpkFileExists = true;
00258 }
00259 catch (SMultiPKFileRep::NotFound)
00260 {
00261 mpkFileExists = false;
00262 }
00263
00264
00265 if (mpkFileExists)
00266 {
00267 try
00268 {
00269 hdr = SMultiPKFile::ReadHeader(pfx, true, ifs);
00270 }
00271 catch (...)
00272 {
00273
00274
00275 cerr << "Exception while reeading MPKFile header "
00276 << FullName(pfx)
00277 << endl;
00278
00279 if (mpkFileExists) FS::Close(ifs);
00280
00281 throw;
00282 }
00283 }
00284 else
00285 {
00286
00287 hdr = NEW(SMultiPKFileRep::Header);
00288 }
00289
00290 return mpkFileExists;
00291 }
00292
00293 void SMultiPKFile::Rewrite(const PKPrefix::T &pfx,
00294
00295
00296 bool mpkFileExists, ifstream &ifs,
00297 SMultiPKFileRep::Header *hdr,
00298
00299 VPKFileMap& vpkFiles,
00300 ChkPtTbl &vpkChkptTbl,
00301 const BitVector *toDelete,
00302 EmptyPKLog *emptyPKLog,
00303 EntryState &state)
00304 throw (FS::Failure, FS::EndOfFile, VestaLog::Error)
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 {
00344 FP::Tag pk;
00345 VPKFile *vpkfile;
00346 VPKFileIter it(&vpkFiles);
00347
00348 AtomicFile ofs;
00349 try {
00350
00351
00352
00353 for (it.Reset(); it.Next( pk, vpkfile); ) {
00354 VPKFileChkPt *chkpt;
00355 SMultiPKFileRep::HeaderEntry *dummyHE;
00356 bool inTbl = vpkChkptTbl.Get(pk, chkpt); assert(inTbl);
00357 if (chkpt->hasNewEntries) {
00358 (void)(hdr->AppendNewHeaderEntry(pk));
00359 } else if (!hdr->pkTbl.Get(pk, dummyHE)) {
00360
00361 inTbl = vpkFiles.Delete(pk, vpkfile, false);
00362 assert(inTbl);
00363 }
00364 }
00365 vpkFiles.Resize();
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 bool l_fileFormatChange = mpkFileExists &&
00377 (hdr->version < SPKFileRep::LastVersion);
00378
00379
00380 int i;
00381 for (i = 0; i < hdr->num; i++) {
00382 FP::Tag *pki = hdr->pkSeq[i];
00383 SMultiPKFileRep::HeaderEntry *he;
00384 bool inTbl = hdr->pkTbl.Get(*pki, he); assert(inTbl);
00385 VPKFileChkPt *chkpt = (VPKFileChkPt *)NULL;
00386 (void)(vpkChkptTbl.Get(he->pk, chkpt));
00387
00388
00389 inTbl = vpkFiles.Get(he->pk, vpkfile); assert(inTbl);
00390
00391
00392
00393 if ((toDelete != (BitVector *)NULL) ||
00394 ((chkpt != (VPKFileChkPt *)NULL) && (chkpt->hasNewEntries))) {
00395
00396
00397 if (he->SPKFileExists()) {
00398
00399 assert(mpkFileExists);
00400 FS::Seek(ifs, he->offset);
00401 he->pkfile = NEW_CONSTR(SPKFile,
00402 (he->pk, ifs, he->offset,
00403 hdr->version, he->pkhdr,
00404 true,
00405 Config_ReadImmutable));
00406
00407 #ifdef MISTRUST_PKLEN
00408
00409 if(FS::Posn(ifs) != (streampos) (he->offset + he->pkLen))
00410 {
00411 Debug::Lock();
00412
00413 cerr << Debug::Timestamp()
00414 << " -- Detected incorrect pkLen" << endl
00415 << " pkfile = " << i << endl
00416 << " expoected position = "
00417 << (he->offset + he->pkLen) << endl
00418 << " actual position = " << FS::Posn(ifs) << endl;
00419 hdr->Debug(cerr, true);
00420
00421 Debug::Unlock();
00422 }
00423 #endif
00424 } else {
00425
00426 he->pkfile = NEW_CONSTR(SPKFile, (he->pk));
00427 }
00428
00429
00430
00431 assert(*(he->pkfile->CommonNames()) == *(vpkfile->CommonNames()));
00432
00433
00434 he->pkfileModified = he->pkfile->Update(chkpt, toDelete,
00435 he->exCommonNames,
00436 he->exUncommonNames,
00437 he->packMask,
00438 he->reMap,
00439 he->becameEmpty);
00440
00441
00442 if (he->becameEmpty) {
00443
00444 hdr->RemoveHeaderEntry(*pki);
00445 i--;
00446
00447
00448 emptyPKLog->Write(*pki, he->pkfile->PKEpoch());
00449 }
00450 } else
00451
00452
00453 if(l_fileFormatChange
00454 #ifdef MISTRUST_PKLEN
00455
00456
00457 || true
00458 #endif
00459 ) {
00460
00461
00462
00463
00464
00465 assert(he->SPKFileExists());
00466
00467
00468 assert(mpkFileExists);
00469 FS::Seek(ifs, he->offset);
00470 he->pkfile = NEW_CONSTR(SPKFile,
00471 (he->pk, ifs, he->offset,
00472 hdr->version, he->pkhdr,
00473 true,
00474 Config_ReadImmutable));
00475
00476
00477
00478 assert(*(he->pkfile->CommonNames()) == *(vpkfile->CommonNames()));
00479
00480 #ifdef MISTRUST_PKLEN
00481
00482 if(FS::Posn(ifs) != (streampos) (he->offset + he->pkLen))
00483 {
00484 Debug::Lock();
00485
00486 cerr << Debug::Timestamp()
00487 << " -- Detected incorrect pkLen:" << endl
00488 << " pkfile = " << i << endl
00489 << " expoected position = "
00490 << (he->offset + he->pkLen) << endl
00491 << " actual position = " << FS::Posn(ifs) << endl;
00492 hdr->Debug(cerr, true);
00493
00494 Debug::Unlock();
00495 }
00496 #endif
00497
00498 } else {
00499 he->pkfileModified = false;
00500 }
00501 }
00502
00503
00504 if (hdr->num > 0) {
00505
00506 OpenAtomicWrite(pfx, ofs);
00507
00508
00509 hdr->Write(ofs);
00510 hdr->WriteEntries(ofs);
00511
00512
00513 for (i = 0; i < hdr->num; i++) {
00514 FP::Tag *pki = hdr->pkSeq[i];
00515 SMultiPKFileRep::HeaderEntry *he;
00516 bool inTbl = hdr->pkTbl.Get(*pki, he); assert(inTbl);
00517 if (he->pkfileModified || l_fileFormatChange
00518 #ifdef MISTRUST_PKLEN
00519
00520
00521 || true
00522 #endif
00523 ) {
00524
00525
00526
00527 he->offset = FS::Posn(ofs);
00528 assert(he->pkfile != NULL);
00529 he->pkfile->Write(ifs, ofs, he->offset, he->pkhdr);
00530 } else {
00531
00532 FS::Seek(ifs, he->offset);
00533 he->offset = FS::Posn(ofs);
00534 FS::Copy(ifs, ofs, he->pkLen);
00535 }
00536 }
00537
00538
00539 hdr->BackPatch(ofs);
00540 for (i = 0; i < hdr->num; i++) {
00541 FP::Tag *pki = hdr->pkSeq[i];
00542 SMultiPKFileRep::HeaderEntry *he;
00543 bool inTbl = hdr->pkTbl.Get(*pki, he); assert(inTbl);
00544
00545
00546
00547
00548
00549
00550 if (he->pkfileModified || l_fileFormatChange
00551 #ifdef MISTRUST_PKLEN
00552
00553
00554
00555 || true
00556 #endif
00557 ) {
00558 assert(he->pkfile != NULL && he->pkhdr != NULL);
00559 he->pkhdr->BackPatch(ofs, he->offset);
00560 }
00561 }
00562 }
00563 }
00564 catch (...)
00565 {
00566
00567
00568 cerr << "Exception while re-writing MPKFile "
00569 << FullName(pfx)
00570 << endl;
00571
00572 if (mpkFileExists) FS::Close(ifs);
00573
00574 throw;
00575 }
00576 if (mpkFileExists) FS::Close(ifs);
00577
00578
00579 if (Config_WeedPauseDur > 0) PauseDeletionThread();
00580
00581
00582 for (it.Reset(); it.Next( pk, vpkfile); ) {
00583
00584 bool inTbl;
00585 SMultiPKFileRep::HeaderEntry *he;
00586 VPKFileChkPt *chkpt;
00587 inTbl = hdr->pkTbl.Get(pk, he); assert(inTbl);
00588 inTbl = vpkChkptTbl.Get(pk, chkpt); assert(inTbl);
00589
00590
00591 vpkfile->mu.lock();
00592
00593
00594
00595
00596 if(vpkfile->Evicted())
00597 {
00598 Debug::Lock();
00599
00600 cerr << Debug::Timestamp()
00601 << "INTERNAL ERROR: "
00602 << "evicted VPKFile in SMultiPKFile::Rewrite:" << endl
00603 << " pk = " << pk << endl
00604 << " (Please report this as a bug.)" << endl;
00605
00606 Debug::Unlock();
00607
00608
00609 abort();
00610 }
00611
00612
00613
00614
00615 if (!he->pkfileModified && !vpkfile->HasNewEntries()) {
00616 vpkfile->DecrPKEpoch();
00617 }
00618
00619
00620
00621
00622
00623
00624 assert(!chkpt->hasNewEntries || he->pkfileModified);
00625 if (he->pkfileModified) {
00626 assert(he->pkfile != (SPKFile *)NULL);
00627
00628
00629 assert((vpkfile->PKEpoch() == he->pkfile->PKEpoch()) &&
00630 (vpkfile->PKEpoch() == (chkpt->pkEpoch + 1)));
00631 vpkfile->Update(*(he->pkfile), *chkpt,
00632 toDelete, he->exCommonNames, he->exUncommonNames,
00633 he->packMask, he->reMap, he->becameEmpty, state);
00634 }
00635
00636
00637
00638
00639 if(he->pkfile != NULL)
00640 {
00641 assert(*(he->pkfile->CommonNames()) == *(vpkfile->CommonNames()));
00642 }
00643 }
00644
00645
00646 if (hdr->num == 0) {
00647
00648
00649
00650 if (hdr->totalLen > 0) DeleteFile(pfx);
00651 } else {
00652 FS::Close(ofs);
00653 }
00654
00655
00656 for (it.Reset(); it.Next( pk, vpkfile); ) {
00657 vpkfile->mu.unlock();
00658 }
00659 }
00660
00661 SMultiPKFileRep::Header* SMultiPKFile::ReadHeader(const PKPrefix::T &pfx,
00662 bool readEntries, ifstream &ifs) throw (FS::Failure, FS::EndOfFile)
00663 {
00664 SMultiPKFileRep::Header *res;
00665 try {
00666
00667 res = NEW_CONSTR(SMultiPKFileRep::Header, (ifs));
00668 if (readEntries) res->ReadEntries(ifs);
00669 }
00670 catch (SMultiPKFileRep::BadMPKFile) {
00671 cerr << "Fatal error: the MultiPKFile for prefix " << pfx
00672 << " is bad; crashing..." << endl;
00673 assert(false);
00674 }
00675 return res;
00676 }
00677
00678 Text SMultiPKFile::Name(const PKPrefix::T &pfx, int granBits) throw ()
00679 {
00680 char buff[10];
00681 int printLen = sprintf(buff, "gran-%02d/", granBits);
00682 assert(printLen == 8);
00683 Text res(buff);
00684 res += pfx.Pathname(granBits, ArcBits);
00685 return res;
00686 }
00687
00688 Text SMultiPKFile::FullName(const PKPrefix::T &pfx) throw ()
00689 {
00690 Text nm(Name(pfx, PKPrefix::Granularity()));
00691 Text res(Config_SCachePath + "/" + nm);
00692 return res;
00693 }
00694
00695 void SMultiPKFile::OpenAtomicWrite(const PKPrefix::T &pfx,
00696 AtomicFile &ofs) throw (FS::Failure)
00697 {
00698
00699 Text fpath(FullName(pfx));
00700
00701
00702 ofs.open(fpath.chars(), ios::out);
00703 if (ofs.fail() && errno == ENOENT) {
00704
00705 MakeDirs(fpath);
00706
00707
00708
00709 ofs.clear(ios::goodbit);
00710
00711
00712 ofs.open(fpath.chars(), ios::out);
00713 }
00714
00715
00716 if (ofs.fail()) {
00717 throw(FS::Failure(Text("OpenAtomicWrite"), fpath));
00718 }
00719 }
00720
00721 void SMultiPKFile::MakeDirs(const Text &name) throw (FS::Failure)
00722 {
00723
00724 const char PathnameSep = '/';
00725 const int DirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
00726
00727
00728 Text nm(name.chars());
00729
00730
00731 const char *start = nm.cchars();
00732 char *q = ((char *)start) + nm.Length();
00733
00734
00735 int stat_res;
00736 do {
00737 struct stat buffer;
00738 for (q--; q >= start && *q != PathnameSep; q--) ;
00739 assert(q >= start);
00740 *q = '\0';
00741 stat_res = stat(start, &buffer);
00742 *q = PathnameSep;
00743 } while (stat_res != 0);
00744
00745
00746 while (true) {
00747 q = strchr(q+1, PathnameSep);
00748 if (q == (char *)NULL) break;
00749 *q = '\0';
00750 int status = mkdir(start, DirMode);
00751 *q = PathnameSep;
00752 if (status < 0 && errno != EEXIST) {
00753 throw (FS::Failure(Text("SMultiPKFile::MakeDirs"), name));
00754 }
00755 }
00756 }
00757
00758 void SMultiPKFile::DeleteFile(const PKPrefix::T &pfx)
00759 throw (FS::Failure)
00760 {
00761
00762 Text fpath(FullName(pfx));
00763 FS::Delete(fpath);
00764
00765
00766 int i = fpath.Length(); bool deleted;
00767 do {
00768 i = fpath.FindCharR('/', i - 1); assert(i >= 0);
00769 Text dir(fpath.Sub(0, i));
00770 deleted = (dir != Config_SCachePath && FS::DeleteDir(dir));
00771 } while (deleted);
00772 }
00773
00774 void SMultiPKFile::PauseDeletionThread() throw ()
00775 {
00776
00777 Debug::Lock();
00778 cout << Debug::Timestamp() << "STARTING -- Pausing Deletion Thread" <<endl;
00779 cout << " Duration = " << Config_WeedPauseDur << " seconds" << endl;
00780 cout << endl;
00781 Debug::Unlock();
00782
00783
00784 Basics::thread::pause(Config_WeedPauseDur);
00785
00786
00787 Debug::Lock();
00788 cout << Debug::Timestamp() << "DONE -- Pausing Deletion Thread" << endl;
00789 cout << endl;
00790 Debug::Unlock();
00791 }