fs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. int rv;
  694. Fs *fs = a;
  695. vtRLock(fs->elk);
  696. rv = fileMetaFlush(fs->file, 1);
  697. vtRUnlock(fs->elk);
  698. if(rv > 0)
  699. cacheFlush(fs->cache, 0);
  700. }
  701. static int
  702. fsEsearch1(File *f, char *path, u32int savetime, u32int *plo)
  703. {
  704. int n, r;
  705. DirEntry de;
  706. DirEntryEnum *dee;
  707. File *ff;
  708. Entry e, ee;
  709. char *t;
  710. dee = deeOpen(f);
  711. if(dee == nil)
  712. return 0;
  713. n = 0;
  714. for(;;){
  715. r = deeRead(dee, &de);
  716. if(r <= 0)
  717. break;
  718. if(de.mode & ModeSnapshot){
  719. if((ff = fileWalk(f, de.elem)) != nil){
  720. if(fileGetSources(ff, &e, &ee))
  721. if(de.mtime >= savetime && e.snap != 0)
  722. if(e.snap < *plo)
  723. *plo = e.snap;
  724. fileDecRef(ff);
  725. }
  726. }
  727. else if(de.mode & ModeDir){
  728. if((ff = fileWalk(f, de.elem)) != nil){
  729. t = smprint("%s/%s", path, de.elem);
  730. n += fsEsearch1(ff, t, savetime, plo);
  731. vtMemFree(t);
  732. fileDecRef(ff);
  733. }
  734. }
  735. deCleanup(&de);
  736. if(r < 0)
  737. break;
  738. }
  739. deeClose(dee);
  740. return n;
  741. }
  742. static int
  743. fsEsearch(Fs *fs, char *path, u32int savetime, u32int *plo)
  744. {
  745. int n;
  746. File *f;
  747. DirEntry de;
  748. f = fileOpen(fs, path);
  749. if(f == nil)
  750. return 0;
  751. if(!fileGetDir(f, &de)){
  752. fileDecRef(f);
  753. return 0;
  754. }
  755. if((de.mode & ModeDir) == 0){
  756. fileDecRef(f);
  757. deCleanup(&de);
  758. return 0;
  759. }
  760. deCleanup(&de);
  761. n = fsEsearch1(f, path, savetime, plo);
  762. fileDecRef(f);
  763. return n;
  764. }
  765. void
  766. fsSnapshotCleanup(Fs *fs, u32int age)
  767. {
  768. u32int lo;
  769. /*
  770. * Find the best low epoch we can use,
  771. * given that we need to save all the unventied archives
  772. * and all the snapshots younger than age.
  773. */
  774. vtRLock(fs->elk);
  775. lo = fs->ehi;
  776. fsEsearch(fs, "/archive", 0, &lo);
  777. fsEsearch(fs, "/snapshot", time(0)-age*60, &lo);
  778. vtRUnlock(fs->elk);
  779. fsEpochLow(fs, lo);
  780. fsSnapshotRemove(fs);
  781. }
  782. /* remove all snapshots that have expired */
  783. /* return number of directory entries remaining */
  784. static int
  785. fsRsearch1(File *f, char *s)
  786. {
  787. int n, r;
  788. DirEntry de;
  789. DirEntryEnum *dee;
  790. File *ff;
  791. char *t;
  792. dee = deeOpen(f);
  793. if(dee == nil)
  794. return 0;
  795. n = 0;
  796. for(;;){
  797. r = deeRead(dee, &de);
  798. if(r <= 0)
  799. break;
  800. n++;
  801. if(de.mode & ModeSnapshot){
  802. if((ff = fileWalk(f, de.elem)) != nil)
  803. fileDecRef(ff);
  804. else if(strcmp(vtGetError(), ESnapOld) == 0){
  805. if(fileClri(f, de.elem, "adm"))
  806. n--;
  807. }
  808. }
  809. else if(de.mode & ModeDir){
  810. if((ff = fileWalk(f, de.elem)) != nil){
  811. t = smprint("%s/%s", s, de.elem);
  812. if(fsRsearch1(ff, t) == 0)
  813. if(fileRemove(ff, "adm"))
  814. n--;
  815. vtMemFree(t);
  816. fileDecRef(ff);
  817. }
  818. }
  819. deCleanup(&de);
  820. if(r < 0)
  821. break;
  822. }
  823. deeClose(dee);
  824. return n;
  825. }
  826. static int
  827. fsRsearch(Fs *fs, char *path)
  828. {
  829. File *f;
  830. DirEntry de;
  831. f = fileOpen(fs, path);
  832. if(f == nil)
  833. return 0;
  834. if(!fileGetDir(f, &de)){
  835. fileDecRef(f);
  836. return 0;
  837. }
  838. if((de.mode & ModeDir) == 0){
  839. fileDecRef(f);
  840. deCleanup(&de);
  841. return 0;
  842. }
  843. deCleanup(&de);
  844. fsRsearch1(f, path);
  845. fileDecRef(f);
  846. return 1;
  847. }
  848. void
  849. fsSnapshotRemove(Fs *fs)
  850. {
  851. vtRLock(fs->elk);
  852. fsRsearch(fs, "/snapshot");
  853. vtRUnlock(fs->elk);
  854. }
  855. struct Snap
  856. {
  857. Fs *fs;
  858. Periodic *tick;
  859. VtLock *lk;
  860. uint snapMinutes;
  861. uint archMinute;
  862. uint snapLife;
  863. u32int lastSnap;
  864. u32int lastArch;
  865. u32int lastCleanup;
  866. uint ignore;
  867. };
  868. static void
  869. snapEvent(void *v)
  870. {
  871. Snap *s;
  872. u32int now, min;
  873. Tm tm;
  874. int need;
  875. u32int snaplife;
  876. s = v;
  877. now = time(0)/60;
  878. vtLock(s->lk);
  879. /*
  880. * Snapshots happen every snapMinutes minutes.
  881. * If we miss a snapshot (for example, because we
  882. * were down), we wait for the next one.
  883. */
  884. if(s->snapMinutes != ~0 && s->snapMinutes != 0
  885. && now%s->snapMinutes==0 && now != s->lastSnap){
  886. if(!fsSnapshot(s->fs, nil, nil, 0))
  887. fprint(2, "fsSnapshot snap: %R\n");
  888. s->lastSnap = now;
  889. }
  890. /*
  891. * Archival snapshots happen at archMinute.
  892. * If we miss an archive (for example, because we
  893. * were down), we do it as soon as possible.
  894. */
  895. tm = *localtime(now*60);
  896. min = tm.hour*60+tm.min;
  897. if(s->archMinute != ~0){
  898. need = 0;
  899. if(min == s->archMinute && now != s->lastArch)
  900. need = 1;
  901. if(s->lastArch == 0){
  902. s->lastArch = 1;
  903. if(fsNeedArch(s->fs, s->archMinute))
  904. need = 1;
  905. }
  906. if(need){
  907. fsSnapshot(s->fs, nil, nil, 1);
  908. s->lastArch = now;
  909. }
  910. }
  911. /*
  912. * Snapshot cleanup happens every snaplife or every day.
  913. */
  914. snaplife = s->snapLife;
  915. if(snaplife == ~0)
  916. snaplife = 24*60;
  917. if(s->lastCleanup+snaplife < now){
  918. fsSnapshotCleanup(s->fs, s->snapLife);
  919. s->lastCleanup = now;
  920. }
  921. vtUnlock(s->lk);
  922. }
  923. static Snap*
  924. snapInit(Fs *fs)
  925. {
  926. Snap *s;
  927. s = vtMemAllocZ(sizeof(Snap));
  928. s->fs = fs;
  929. s->tick = periodicAlloc(snapEvent, s, 10*1000);
  930. s->lk = vtLockAlloc();
  931. s->snapMinutes = -1;
  932. s->archMinute = -1;
  933. s->snapLife = -1;
  934. s->ignore = 5*2; /* wait five minutes for clock to stabilize */
  935. return s;
  936. }
  937. void
  938. snapGetTimes(Snap *s, u32int *arch, u32int *snap, u32int *snaplen)
  939. {
  940. if(s == nil){
  941. *snap = -1;
  942. *arch = -1;
  943. *snaplen = -1;
  944. return;
  945. }
  946. vtLock(s->lk);
  947. *snap = s->snapMinutes;
  948. *arch = s->archMinute;
  949. *snaplen = s->snapLife;
  950. vtUnlock(s->lk);
  951. }
  952. void
  953. snapSetTimes(Snap *s, u32int arch, u32int snap, u32int snaplen)
  954. {
  955. if(s == nil)
  956. return;
  957. vtLock(s->lk);
  958. s->snapMinutes = snap;
  959. s->archMinute = arch;
  960. s->snapLife = snaplen;
  961. vtUnlock(s->lk);
  962. }
  963. static void
  964. snapClose(Snap *s)
  965. {
  966. if(s == nil)
  967. return;
  968. periodicKill(s->tick);
  969. vtMemFree(s);
  970. }