fs.c 21 KB

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