fs.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. #include "error.h"
  5. static void fsMetaFlush(void *a);
  6. static Snap *snapInit(Fs*);
  7. static void snapClose(Snap*);
  8. Fs *
  9. fsOpen(char *file, VtSession *z, long ncache, int mode)
  10. {
  11. int fd, m;
  12. uchar oscore[VtScoreSize];
  13. Block *b, *bs;
  14. Disk *disk;
  15. Fs *fs;
  16. Super super;
  17. switch(mode){
  18. default:
  19. vtSetError(EBadMode);
  20. return nil;
  21. case OReadOnly:
  22. m = OREAD;
  23. break;
  24. case OReadWrite:
  25. m = ORDWR;
  26. break;
  27. }
  28. fd = open(file, m);
  29. if(fd < 0){
  30. vtSetError("open %s: %r", file);
  31. return nil;
  32. }
  33. bwatchInit();
  34. disk = diskAlloc(fd);
  35. if(disk == nil){
  36. vtSetError("diskAlloc: %R");
  37. close(fd);
  38. return nil;
  39. }
  40. fs = vtMemAllocZ(sizeof(Fs));
  41. fs->mode = mode;
  42. fs->name = vtStrDup(file);
  43. fs->blockSize = diskBlockSize(disk);
  44. fs->elk = vtLockAlloc();
  45. fs->cache = cacheAlloc(disk, z, ncache, mode);
  46. if(mode == OReadWrite && z)
  47. fs->arch = archInit(fs->cache, disk, fs, z);
  48. fs->z = z;
  49. b = cacheLocal(fs->cache, PartSuper, 0, mode);
  50. if(b == nil)
  51. goto Err;
  52. if(!superUnpack(&super, b->data)){
  53. blockPut(b);
  54. vtSetError("bad super block");
  55. goto Err;
  56. }
  57. blockPut(b);
  58. fs->ehi = super.epochHigh;
  59. fs->elo = super.epochLow;
  60. //fprint(2, "%s: fs->ehi %d fs->elo %d active=%d\n", argv0, fs->ehi, fs->elo, super.active);
  61. fs->source = sourceRoot(fs, super.active, mode);
  62. if(fs->source == nil){
  63. /*
  64. * Perhaps it failed because the block is copy-on-write.
  65. * Do the copy and try again.
  66. */
  67. if(mode == OReadOnly || strcmp(vtGetError(), EBadRoot) != 0)
  68. goto Err;
  69. b = cacheLocalData(fs->cache, super.active, BtDir, RootTag,
  70. OReadWrite, 0);
  71. if(b == nil){
  72. vtSetError("cacheLocalData: %R");
  73. goto Err;
  74. }
  75. if(b->l.epoch == fs->ehi){
  76. blockPut(b);
  77. vtSetError("bad root source block");
  78. goto Err;
  79. }
  80. b = blockCopy(b, RootTag, fs->ehi, fs->elo);
  81. if(b == nil)
  82. goto Err;
  83. localToGlobal(super.active, oscore);
  84. super.active = b->addr;
  85. bs = cacheLocal(fs->cache, PartSuper, 0, OReadWrite);
  86. if(bs == nil){
  87. blockPut(b);
  88. vtSetError("cacheLocal: %R");
  89. goto Err;
  90. }
  91. superPack(&super, bs->data);
  92. blockDependency(bs, b, 0, oscore, nil);
  93. blockPut(b);
  94. blockDirty(bs);
  95. blockRemoveLink(bs, globalToLocal(oscore), BtDir, RootTag, 0);
  96. blockPut(bs);
  97. fs->source = sourceRoot(fs, super.active, mode);
  98. if(fs->source == nil){
  99. vtSetError("sourceRoot: %R");
  100. goto Err;
  101. }
  102. }
  103. //fprint(2, "%s: got fs source\n", argv0);
  104. vtRLock(fs->elk);
  105. fs->file = fileRoot(fs->source);
  106. fs->source->file = fs->file; /* point back */
  107. vtRUnlock(fs->elk);
  108. if(fs->file == nil){
  109. vtSetError("fileRoot: %R");
  110. goto Err;
  111. }
  112. //fprint(2, "%s: got file root\n", argv0);
  113. if(mode == OReadWrite){
  114. fs->metaFlush = periodicAlloc(fsMetaFlush, fs, 1000);
  115. fs->snap = snapInit(fs);
  116. }
  117. return fs;
  118. Err:
  119. fprint(2, "%s: fsOpen error\n", argv0);
  120. fsClose(fs);
  121. return nil;
  122. }
  123. void
  124. fsClose(Fs *fs)
  125. {
  126. vtRLock(fs->elk);
  127. periodicKill(fs->metaFlush);
  128. snapClose(fs->snap);
  129. if(fs->file){
  130. fileMetaFlush(fs->file, 0);
  131. if(!fileDecRef(fs->file))
  132. vtFatal("fsClose: files still in use: %r\n");
  133. }
  134. fs->file = nil;
  135. sourceClose(fs->source);
  136. cacheFree(fs->cache);
  137. if(fs->arch)
  138. archFree(fs->arch);
  139. vtMemFree(fs->name);
  140. vtRUnlock(fs->elk);
  141. vtLockFree(fs->elk);
  142. memset(fs, ~0, sizeof(Fs));
  143. vtMemFree(fs);
  144. }
  145. int
  146. fsRedial(Fs *fs, char *host)
  147. {
  148. if(!vtRedial(fs->z, host))
  149. return 0;
  150. if(!vtConnect(fs->z, 0))
  151. return 0;
  152. return 1;
  153. }
  154. File *
  155. fsGetRoot(Fs *fs)
  156. {
  157. return fileIncRef(fs->file);
  158. }
  159. int
  160. fsGetBlockSize(Fs *fs)
  161. {
  162. return fs->blockSize;
  163. }
  164. Block*
  165. superGet(Cache *c, Super* super)
  166. {
  167. Block *b;
  168. if((b = cacheLocal(c, PartSuper, 0, OReadWrite)) == nil){
  169. fprint(2, "%s: superGet: cacheLocal failed: %R\n", argv0);
  170. return nil;
  171. }
  172. if(!superUnpack(super, b->data)){
  173. fprint(2, "%s: superGet: superUnpack failed: %R\n", argv0);
  174. blockPut(b);
  175. return nil;
  176. }
  177. return b;
  178. }
  179. void
  180. superWrite(Block* b, Super* super, int forceWrite)
  181. {
  182. superPack(super, b->data);
  183. blockDirty(b);
  184. if(forceWrite){
  185. while(!blockWrite(b, Waitlock)){
  186. /* this should no longer happen */
  187. fprint(2, "%s: could not write super block; "
  188. "waiting 10 seconds\n", argv0);
  189. sleep(10*1000);
  190. }
  191. while(b->iostate != BioClean && b->iostate != BioDirty){
  192. assert(b->iostate == BioWriting);
  193. vtSleep(b->ioready);
  194. }
  195. /*
  196. * it's okay that b might still be dirty.
  197. * that means it got written out but with an old root pointer,
  198. * but the other fields went out, and those are the ones
  199. * we really care about. (specifically, epochHigh; see fsSnapshot).
  200. */
  201. }
  202. }
  203. /*
  204. * Prepare the directory to store a snapshot.
  205. * Temporary snapshots go into /snapshot/yyyy/mmdd/hhmm[.#]
  206. * Archival snapshots go into /archive/yyyy/mmdd[.#].
  207. *
  208. * TODO This should be rewritten to eliminate most of the duplication.
  209. */
  210. static File*
  211. fileOpenSnapshot(Fs *fs, char *dstpath, int doarchive)
  212. {
  213. int n;
  214. char buf[30], *s, *p, *elem;
  215. File *dir, *f;
  216. Tm now;
  217. if(dstpath){
  218. if((p = strrchr(dstpath, '/')) != nil){
  219. *p++ = '\0';
  220. elem = p;
  221. p = dstpath;
  222. if(*p == '\0')
  223. p = "/";
  224. }else{
  225. p = "/";
  226. elem = dstpath;
  227. }
  228. if((dir = fileOpen(fs, p)) == nil)
  229. return nil;
  230. f = fileCreate(dir, elem, ModeDir|ModeSnapshot|0555, "adm");
  231. fileDecRef(dir);
  232. return f;
  233. }else if(doarchive){
  234. /*
  235. * a snapshot intended to be archived to venti.
  236. */
  237. dir = fileOpen(fs, "/archive");
  238. if(dir == nil)
  239. return nil;
  240. now = *localtime(time(0));
  241. /* yyyy */
  242. snprint(buf, sizeof(buf), "%d", now.year+1900);
  243. f = fileWalk(dir, buf);
  244. if(f == nil)
  245. f = fileCreate(dir, buf, ModeDir|0555, "adm");
  246. fileDecRef(dir);
  247. if(f == nil)
  248. return nil;
  249. dir = f;
  250. /* mmdd[#] */
  251. snprint(buf, sizeof(buf), "%02d%02d", now.mon+1, now.mday);
  252. s = buf+strlen(buf);
  253. for(n=0;; n++){
  254. if(n)
  255. seprint(s, buf+sizeof(buf), ".%d", n);
  256. f = fileWalk(dir, buf);
  257. if(f != nil){
  258. fileDecRef(f);
  259. continue;
  260. }
  261. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  262. break;
  263. }
  264. fileDecRef(dir);
  265. return f;
  266. }else{
  267. /*
  268. * Just a temporary snapshot
  269. * We'll use /snapshot/yyyy/mmdd/hhmm.
  270. * There may well be a better naming scheme.
  271. * (I'd have used hh:mm but ':' is reserved in Microsoft file systems.)
  272. */
  273. dir = fileOpen(fs, "/snapshot");
  274. if(dir == nil)
  275. return nil;
  276. now = *localtime(time(0));
  277. /* yyyy */
  278. snprint(buf, sizeof(buf), "%d", now.year+1900);
  279. f = fileWalk(dir, buf);
  280. if(f == nil)
  281. f = fileCreate(dir, buf, ModeDir|0555, "adm");
  282. fileDecRef(dir);
  283. if(f == nil)
  284. return nil;
  285. dir = f;
  286. /* mmdd */
  287. snprint(buf, sizeof(buf), "%02d%02d", now.mon+1, now.mday);
  288. f = fileWalk(dir, buf);
  289. if(f == nil)
  290. f = fileCreate(dir, buf, ModeDir|0555, "adm");
  291. fileDecRef(dir);
  292. if(f == nil)
  293. return nil;
  294. dir = f;
  295. /* hhmm[.#] */
  296. snprint(buf, sizeof buf, "%02d%02d", now.hour, now.min);
  297. s = buf+strlen(buf);
  298. for(n=0;; n++){
  299. if(n)
  300. seprint(s, buf+sizeof(buf), ".%d", n);
  301. f = fileWalk(dir, buf);
  302. if(f != nil){
  303. fileDecRef(f);
  304. continue;
  305. }
  306. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  307. break;
  308. }
  309. fileDecRef(dir);
  310. return f;
  311. }
  312. }
  313. static int
  314. fsNeedArch(Fs *fs, uint archMinute)
  315. {
  316. int need;
  317. File *f;
  318. char buf[100];
  319. Tm now;
  320. ulong then;
  321. then = time(0);
  322. now = *localtime(then);
  323. /* back up to yesterday if necessary */
  324. if(now.hour < archMinute/60
  325. || now.hour == archMinute/60 && now.min < archMinute%60)
  326. now = *localtime(then-86400);
  327. snprint(buf, sizeof buf, "/archive/%d/%02d%02d",
  328. now.year+1900, now.mon+1, now.mday);
  329. need = 1;
  330. vtRLock(fs->elk);
  331. f = fileOpen(fs, buf);
  332. if(f){
  333. need = 0;
  334. fileDecRef(f);
  335. }
  336. vtRUnlock(fs->elk);
  337. return need;
  338. }
  339. int
  340. fsEpochLow(Fs *fs, u32int low)
  341. {
  342. Block *bs;
  343. Super super;
  344. vtLock(fs->elk);
  345. if(low > fs->ehi){
  346. vtSetError("bad low epoch (must be <= %ud)", fs->ehi);
  347. vtUnlock(fs->elk);
  348. return 0;
  349. }
  350. if((bs = superGet(fs->cache, &super)) == nil){
  351. vtUnlock(fs->elk);
  352. return 0;
  353. }
  354. super.epochLow = low;
  355. fs->elo = low;
  356. superWrite(bs, &super, 1);
  357. blockPut(bs);
  358. vtUnlock(fs->elk);
  359. return 1;
  360. }
  361. static int
  362. bumpEpoch(Fs *fs, int doarchive)
  363. {
  364. uchar oscore[VtScoreSize];
  365. u32int oldaddr;
  366. Block *b, *bs;
  367. Entry e;
  368. Source *r;
  369. Super super;
  370. /*
  371. * Duplicate the root block.
  372. *
  373. * As a hint to flchk, the garbage collector,
  374. * and any (human) debuggers, store a pointer
  375. * to the old root block in entry 1 of the new root block.
  376. */
  377. r = fs->source;
  378. b = cacheGlobal(fs->cache, r->score, BtDir, RootTag, OReadOnly);
  379. if(b == nil)
  380. return 0;
  381. memset(&e, 0, sizeof e);
  382. e.flags = VtEntryActive | VtEntryLocal | VtEntryDir;
  383. memmove(e.score, b->score, VtScoreSize);
  384. e.tag = RootTag;
  385. e.snap = b->l.epoch;
  386. b = blockCopy(b, RootTag, fs->ehi+1, fs->elo);
  387. if(b == nil){
  388. fprint(2, "%s: bumpEpoch: blockCopy: %R\n", argv0);
  389. return 0;
  390. }
  391. if(0) fprint(2, "%s: snapshot root from %d to %d\n", argv0, oldaddr, b->addr);
  392. entryPack(&e, b->data, 1);
  393. blockDirty(b);
  394. /*
  395. * Update the superblock with the new root and epoch.
  396. */
  397. if((bs = superGet(fs->cache, &super)) == nil)
  398. return 0;
  399. fs->ehi++;
  400. memmove(r->score, b->score, VtScoreSize);
  401. r->epoch = fs->ehi;
  402. super.epochHigh = fs->ehi;
  403. oldaddr = super.active;
  404. super.active = b->addr;
  405. if(doarchive)
  406. super.next = oldaddr;
  407. /*
  408. * Record that the new super.active can't get written out until
  409. * the new b gets written out. Until then, use the old value.
  410. */
  411. localToGlobal(oldaddr, oscore);
  412. blockDependency(bs, b, 0, oscore, nil);
  413. blockPut(b);
  414. /*
  415. * We force the super block to disk so that super.epochHigh gets updated.
  416. * Otherwise, if we crash and come back, we might incorrectly treat as active
  417. * some of the blocks that making up the snapshot we just created.
  418. * Basically every block in the active file system and all the blocks in
  419. * the recently-created snapshot depend on the super block now.
  420. * Rather than record all those dependencies, we just force the block to disk.
  421. *
  422. * Note that blockWrite might actually (will probably) send a slightly outdated
  423. * super.active to disk. It will be the address of the most recent root that has
  424. * gone to disk.
  425. */
  426. superWrite(bs, &super, 1);
  427. blockRemoveLink(bs, globalToLocal(oscore), BtDir, RootTag, 0);
  428. blockPut(bs);
  429. return 1;
  430. }
  431. int
  432. saveQid(Fs *fs)
  433. {
  434. Block *b;
  435. Super super;
  436. u64int qidMax;
  437. if((b = superGet(fs->cache, &super)) == nil)
  438. return 0;
  439. qidMax = super.qid;
  440. blockPut(b);
  441. if(!fileSetQidSpace(fs->file, 0, qidMax))
  442. return 0;
  443. return 1;
  444. }
  445. int
  446. fsSnapshot(Fs *fs, char *srcpath, char *dstpath, int doarchive)
  447. {
  448. File *src, *dst;
  449. assert(fs->mode == OReadWrite);
  450. dst = nil;
  451. if(fs->halted){
  452. vtSetError("file system is halted");
  453. return 0;
  454. }
  455. /*
  456. * Freeze file system activity.
  457. */
  458. vtLock(fs->elk);
  459. /*
  460. * Get the root of the directory we're going to save.
  461. */
  462. if(srcpath == nil)
  463. srcpath = "/active";
  464. src = fileOpen(fs, srcpath);
  465. if(src == nil)
  466. goto Err;
  467. /*
  468. * It is important that we maintain the invariant that:
  469. * if both b and bb are marked as Active with start epoch e
  470. * and b points at bb, then no other pointers to bb exist.
  471. *
  472. * When bb is unlinked from b, its close epoch is set to b's epoch.
  473. * A block with epoch == close epoch is
  474. * treated as free by cacheAllocBlock; this aggressively
  475. * reclaims blocks after they have been stored to Venti.
  476. *
  477. * Let's say src->source is block sb, and src->msource is block
  478. * mb. Let's also say that block b holds the Entry structures for
  479. * both src->source and src->msource (their Entry structures might
  480. * be in different blocks, but the argument is the same).
  481. * That is, right now we have:
  482. *
  483. * b Active w/ epoch e, holds ptrs to sb and mb.
  484. * sb Active w/ epoch e.
  485. * mb Active w/ epoch e.
  486. *
  487. * With things as they are now, the invariant requires that
  488. * b holds the only pointers to sb and mb. We want to record
  489. * pointers to sb and mb in new Entries corresponding to dst,
  490. * which breaks the invariant. Thus we need to do something
  491. * about b. Specifically, we bump the file system's epoch and
  492. * then rewalk the path from the root down to and including b.
  493. * This will copy-on-write as we walk, so now the state will be:
  494. *
  495. * b Snap w/ epoch e, holds ptrs to sb and mb.
  496. * new-b Active w/ epoch e+1, holds ptrs to sb and mb.
  497. * sb Active w/ epoch e.
  498. * mb Active w/ epoch e.
  499. *
  500. * In this state, it's perfectly okay to make more pointers to sb and mb.
  501. */
  502. if(!bumpEpoch(fs, 0) || !fileWalkSources(src))
  503. goto Err;
  504. /*
  505. * Sync to disk. I'm not sure this is necessary, but better safe than sorry.
  506. */
  507. cacheFlush(fs->cache, 1);
  508. /*
  509. * Create the directory where we will store the copy of src.
  510. */
  511. dst = fileOpenSnapshot(fs, dstpath, doarchive);
  512. if(dst == nil)
  513. goto Err;
  514. /*
  515. * Actually make the copy by setting dst's source and msource
  516. * to be src's.
  517. */
  518. if(!fileSnapshot(dst, src, fs->ehi-1, doarchive))
  519. goto Err;
  520. fileDecRef(src);
  521. fileDecRef(dst);
  522. src = nil;
  523. dst = nil;
  524. /*
  525. * Make another copy of the file system. This one is for the
  526. * archiver, so that the file system we archive has the recently
  527. * added snapshot both in /active and in /archive/yyyy/mmdd[.#].
  528. */
  529. if(doarchive){
  530. if(!saveQid(fs))
  531. goto Err;
  532. if(!bumpEpoch(fs, 1))
  533. goto Err;
  534. }
  535. vtUnlock(fs->elk);
  536. /* BUG? can fs->arch fall out from under us here? */
  537. if(doarchive && fs->arch)
  538. archKick(fs->arch);
  539. return 1;
  540. Err:
  541. fprint(2, "%s: fsSnapshot: %R\n", argv0);
  542. if(src)
  543. fileDecRef(src);
  544. if(dst)
  545. fileDecRef(dst);
  546. vtUnlock(fs->elk);
  547. return 0;
  548. }
  549. int
  550. fsVac(Fs *fs, char *name, uchar score[VtScoreSize])
  551. {
  552. int r;
  553. DirEntry de;
  554. Entry e, ee;
  555. File *f;
  556. vtRLock(fs->elk);
  557. f = fileOpen(fs, name);
  558. if(f == nil){
  559. vtRUnlock(fs->elk);
  560. return 0;
  561. }
  562. if(!fileGetSources(f, &e, &ee) || !fileGetDir(f, &de)){
  563. fileDecRef(f);
  564. vtRUnlock(fs->elk);
  565. return 0;
  566. }
  567. fileDecRef(f);
  568. r = mkVac(fs->z, fs->blockSize, &e, &ee, &de, score);
  569. vtRUnlock(fs->elk);
  570. return r;
  571. }
  572. static int
  573. vtWriteBlock(VtSession *z, uchar *buf, uint n, uint type, uchar score[VtScoreSize])
  574. {
  575. if(!vtWrite(z, score, type, buf, n))
  576. return 0;
  577. if(!vtSha1Check(score, buf, n))
  578. return 0;
  579. return 1;
  580. }
  581. int
  582. mkVac(VtSession *z, uint blockSize, Entry *pe, Entry *pee, DirEntry *pde, uchar score[VtScoreSize])
  583. {
  584. uchar buf[8192];
  585. int i;
  586. uchar *p;
  587. uint n;
  588. DirEntry de;
  589. Entry e, ee, eee;
  590. MetaBlock mb;
  591. MetaEntry me;
  592. VtRoot root;
  593. e = *pe;
  594. ee = *pee;
  595. de = *pde;
  596. if(globalToLocal(e.score) != NilBlock
  597. || (ee.flags&VtEntryActive && globalToLocal(ee.score) != NilBlock)){
  598. vtSetError("can only vac paths already stored on venti");
  599. return 0;
  600. }
  601. /*
  602. * Build metadata source for root.
  603. */
  604. n = deSize(&de);
  605. if(n+MetaHeaderSize+MetaIndexSize > sizeof buf){
  606. vtSetError("DirEntry too big");
  607. return 0;
  608. }
  609. memset(buf, 0, sizeof buf);
  610. mbInit(&mb, buf, n+MetaHeaderSize+MetaIndexSize, 1);
  611. p = mbAlloc(&mb, n);
  612. if(p == nil)
  613. abort();
  614. mbSearch(&mb, de.elem, &i, &me);
  615. assert(me.p == nil);
  616. me.p = p;
  617. me.size = n;
  618. dePack(&de, &me);
  619. mbInsert(&mb, i, &me);
  620. mbPack(&mb);
  621. eee.size = n+MetaHeaderSize+MetaIndexSize;
  622. if(!vtWriteBlock(z, buf, eee.size, VtDataType, eee.score))
  623. return 0;
  624. eee.psize = 8192;
  625. eee.dsize = 8192;
  626. eee.depth = 0;
  627. eee.flags = VtEntryActive;
  628. /*
  629. * Build root source with three entries in it.
  630. */
  631. entryPack(&e, buf, 0);
  632. entryPack(&ee, buf, 1);
  633. entryPack(&eee, buf, 2);
  634. n = VtEntrySize*3;
  635. memset(&root, 0, sizeof root);
  636. if(!vtWriteBlock(z, buf, n, VtDirType, root.score))
  637. return 0;
  638. /*
  639. * Save root.
  640. */
  641. root.version = VtRootVersion;
  642. strecpy(root.type, root.type+sizeof root.type, "vac");
  643. strecpy(root.name, root.name+sizeof root.name, de.elem);
  644. root.blockSize = blockSize;
  645. vtRootPack(&root, buf);
  646. if(!vtWriteBlock(z, buf, VtRootSize, VtRootType, score))
  647. return 0;
  648. return 1;
  649. }
  650. int
  651. fsSync(Fs *fs)
  652. {
  653. vtLock(fs->elk);
  654. fileMetaFlush(fs->file, 1);
  655. cacheFlush(fs->cache, 1);
  656. vtUnlock(fs->elk);
  657. return 1;
  658. }
  659. int
  660. fsHalt(Fs *fs)
  661. {
  662. vtLock(fs->elk);
  663. fs->halted = 1;
  664. fileMetaFlush(fs->file, 1);
  665. cacheFlush(fs->cache, 1);
  666. return 1;
  667. }
  668. int
  669. fsUnhalt(Fs *fs)
  670. {
  671. if(!fs->halted)
  672. return 0;
  673. fs->halted = 0;
  674. vtUnlock(fs->elk);
  675. return 1;
  676. }
  677. int
  678. fsNextQid(Fs *fs, u64int *qid)
  679. {
  680. Block *b;
  681. Super super;
  682. if((b = superGet(fs->cache, &super)) == nil)
  683. return 0;
  684. *qid = super.qid++;
  685. /*
  686. * It's okay if the super block doesn't go to disk immediately,
  687. * since fileMetaAlloc will record a dependency between the
  688. * block holding this qid and the super block. See file.c:/^fileMetaAlloc.
  689. */
  690. superWrite(b, &super, 0);
  691. blockPut(b);
  692. return 1;
  693. }
  694. static void
  695. fsMetaFlush(void *a)
  696. {
  697. int rv;
  698. Fs *fs = a;
  699. vtRLock(fs->elk);
  700. rv = fileMetaFlush(fs->file, 1);
  701. vtRUnlock(fs->elk);
  702. if(rv > 0)
  703. cacheFlush(fs->cache, 0);
  704. }
  705. static int
  706. fsEsearch1(File *f, char *path, u32int savetime, u32int *plo)
  707. {
  708. int n, r;
  709. DirEntry de;
  710. DirEntryEnum *dee;
  711. File *ff;
  712. Entry e, ee;
  713. char *t;
  714. dee = deeOpen(f);
  715. if(dee == nil)
  716. return 0;
  717. n = 0;
  718. for(;;){
  719. r = deeRead(dee, &de);
  720. if(r <= 0)
  721. break;
  722. if(de.mode & ModeSnapshot){
  723. if((ff = fileWalk(f, de.elem)) != nil){
  724. if(fileGetSources(ff, &e, &ee))
  725. if(de.mtime >= savetime && e.snap != 0)
  726. if(e.snap < *plo)
  727. *plo = e.snap;
  728. fileDecRef(ff);
  729. }
  730. }
  731. else if(de.mode & ModeDir){
  732. if((ff = fileWalk(f, de.elem)) != nil){
  733. t = smprint("%s/%s", path, de.elem);
  734. n += fsEsearch1(ff, t, savetime, plo);
  735. vtMemFree(t);
  736. fileDecRef(ff);
  737. }
  738. }
  739. deCleanup(&de);
  740. if(r < 0)
  741. break;
  742. }
  743. deeClose(dee);
  744. return n;
  745. }
  746. static int
  747. fsEsearch(Fs *fs, char *path, u32int savetime, u32int *plo)
  748. {
  749. int n;
  750. File *f;
  751. DirEntry de;
  752. f = fileOpen(fs, path);
  753. if(f == nil)
  754. return 0;
  755. if(!fileGetDir(f, &de)){
  756. fileDecRef(f);
  757. return 0;
  758. }
  759. if((de.mode & ModeDir) == 0){
  760. fileDecRef(f);
  761. deCleanup(&de);
  762. return 0;
  763. }
  764. deCleanup(&de);
  765. n = fsEsearch1(f, path, savetime, plo);
  766. fileDecRef(f);
  767. return n;
  768. }
  769. void
  770. fsSnapshotCleanup(Fs *fs, u32int age)
  771. {
  772. u32int lo;
  773. /*
  774. * Find the best low epoch we can use,
  775. * given that we need to save all the unventied archives
  776. * and all the snapshots younger than age.
  777. */
  778. vtRLock(fs->elk);
  779. lo = fs->ehi;
  780. fsEsearch(fs, "/archive", 0, &lo);
  781. fsEsearch(fs, "/snapshot", time(0)-age*60, &lo);
  782. vtRUnlock(fs->elk);
  783. fsEpochLow(fs, lo);
  784. fsSnapshotRemove(fs);
  785. }
  786. /* remove all snapshots that have expired */
  787. /* return number of directory entries remaining */
  788. static int
  789. fsRsearch1(File *f, char *s)
  790. {
  791. int n, r;
  792. DirEntry de;
  793. DirEntryEnum *dee;
  794. File *ff;
  795. char *t;
  796. dee = deeOpen(f);
  797. if(dee == nil)
  798. return 0;
  799. n = 0;
  800. for(;;){
  801. r = deeRead(dee, &de);
  802. if(r <= 0)
  803. break;
  804. n++;
  805. if(de.mode & ModeSnapshot){
  806. if((ff = fileWalk(f, de.elem)) != nil)
  807. fileDecRef(ff);
  808. else if(strcmp(vtGetError(), ESnapOld) == 0){
  809. if(fileClri(f, de.elem, "adm"))
  810. n--;
  811. }
  812. }
  813. else if(de.mode & ModeDir){
  814. if((ff = fileWalk(f, de.elem)) != nil){
  815. t = smprint("%s/%s", s, de.elem);
  816. if(fsRsearch1(ff, t) == 0)
  817. if(fileRemove(ff, "adm"))
  818. n--;
  819. vtMemFree(t);
  820. fileDecRef(ff);
  821. }
  822. }
  823. deCleanup(&de);
  824. if(r < 0)
  825. break;
  826. }
  827. deeClose(dee);
  828. return n;
  829. }
  830. static int
  831. fsRsearch(Fs *fs, char *path)
  832. {
  833. File *f;
  834. DirEntry de;
  835. f = fileOpen(fs, path);
  836. if(f == nil)
  837. return 0;
  838. if(!fileGetDir(f, &de)){
  839. fileDecRef(f);
  840. return 0;
  841. }
  842. if((de.mode & ModeDir) == 0){
  843. fileDecRef(f);
  844. deCleanup(&de);
  845. return 0;
  846. }
  847. deCleanup(&de);
  848. fsRsearch1(f, path);
  849. fileDecRef(f);
  850. return 1;
  851. }
  852. void
  853. fsSnapshotRemove(Fs *fs)
  854. {
  855. vtRLock(fs->elk);
  856. fsRsearch(fs, "/snapshot");
  857. vtRUnlock(fs->elk);
  858. }
  859. struct Snap
  860. {
  861. Fs *fs;
  862. Periodic*tick;
  863. VtLock *lk;
  864. uint snapMinutes;
  865. uint archMinute;
  866. uint snapLife;
  867. u32int lastSnap;
  868. u32int lastArch;
  869. u32int lastCleanup;
  870. uint ignore;
  871. };
  872. static void
  873. snapEvent(void *v)
  874. {
  875. Snap *s;
  876. u32int now, min;
  877. Tm tm;
  878. int need;
  879. u32int snaplife;
  880. s = v;
  881. now = time(0)/60;
  882. vtLock(s->lk);
  883. /*
  884. * Snapshots happen every snapMinutes minutes.
  885. * If we miss a snapshot (for example, because we
  886. * were down), we wait for the next one.
  887. */
  888. if(s->snapMinutes != ~0 && s->snapMinutes != 0
  889. && now%s->snapMinutes==0 && now != s->lastSnap){
  890. if(!fsSnapshot(s->fs, nil, nil, 0))
  891. fprint(2, "%s: fsSnapshot snap: %R\n", argv0);
  892. s->lastSnap = now;
  893. }
  894. /*
  895. * Archival snapshots happen at archMinute.
  896. * If we miss an archive (for example, because we
  897. * were down), we do it as soon as possible.
  898. */
  899. tm = *localtime(now*60);
  900. min = tm.hour*60+tm.min;
  901. if(s->archMinute != ~0){
  902. need = 0;
  903. if(min == s->archMinute && now != s->lastArch)
  904. need = 1;
  905. if(s->lastArch == 0){
  906. s->lastArch = 1;
  907. if(fsNeedArch(s->fs, s->archMinute))
  908. need = 1;
  909. }
  910. if(need){
  911. fsSnapshot(s->fs, nil, nil, 1);
  912. s->lastArch = now;
  913. }
  914. }
  915. /*
  916. * Snapshot cleanup happens every snaplife or every day.
  917. */
  918. snaplife = s->snapLife;
  919. if(snaplife == ~0)
  920. snaplife = 24*60;
  921. if(s->lastCleanup+snaplife < now){
  922. fsSnapshotCleanup(s->fs, s->snapLife);
  923. s->lastCleanup = now;
  924. }
  925. vtUnlock(s->lk);
  926. }
  927. static Snap*
  928. snapInit(Fs *fs)
  929. {
  930. Snap *s;
  931. s = vtMemAllocZ(sizeof(Snap));
  932. s->fs = fs;
  933. s->tick = periodicAlloc(snapEvent, s, 10*1000);
  934. s->lk = vtLockAlloc();
  935. s->snapMinutes = -1;
  936. s->archMinute = -1;
  937. s->snapLife = -1;
  938. s->ignore = 5*2; /* wait five minutes for clock to stabilize */
  939. return s;
  940. }
  941. void
  942. snapGetTimes(Snap *s, u32int *arch, u32int *snap, u32int *snaplen)
  943. {
  944. if(s == nil){
  945. *snap = -1;
  946. *arch = -1;
  947. *snaplen = -1;
  948. return;
  949. }
  950. vtLock(s->lk);
  951. *snap = s->snapMinutes;
  952. *arch = s->archMinute;
  953. *snaplen = s->snapLife;
  954. vtUnlock(s->lk);
  955. }
  956. void
  957. snapSetTimes(Snap *s, u32int arch, u32int snap, u32int snaplen)
  958. {
  959. if(s == nil)
  960. return;
  961. vtLock(s->lk);
  962. s->snapMinutes = snap;
  963. s->archMinute = arch;
  964. s->snapLife = snaplen;
  965. vtUnlock(s->lk);
  966. }
  967. static void
  968. snapClose(Snap *s)
  969. {
  970. if(s == nil)
  971. return;
  972. periodicKill(s->tick);
  973. vtMemFree(s);
  974. }