fs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. vtOSError();
  32. return nil;
  33. }
  34. bwatchInit();
  35. disk = diskAlloc(fd);
  36. if(disk == nil){
  37. close(fd);
  38. return nil;
  39. }
  40. fs = vtMemAllocZ(sizeof(Fs));
  41. fs->mode = mode;
  42. fs->blockSize = diskBlockSize(disk);
  43. fs->elk = vtLockAlloc();
  44. fs->cache = cacheAlloc(disk, z, ncache, mode);
  45. if(mode == OReadWrite)
  46. fs->arch = archInit(fs->cache, disk, fs, z);
  47. fs->z = z;
  48. b = cacheLocal(fs->cache, PartSuper, 0, mode);
  49. if(b == nil)
  50. goto Err;
  51. if(!superUnpack(&super, b->data)){
  52. blockPut(b);
  53. goto Err;
  54. }
  55. blockPut(b);
  56. fs->ehi = super.epochHigh;
  57. fs->elo = super.epochLow;
  58. fprint(2, "fs->ehi %d fs->elo %d active=%d\n", fs->ehi, fs->elo, super.active);
  59. fs->source = sourceRoot(fs, super.active, mode);
  60. if(fs->source == nil){
  61. /*
  62. * Perhaps it failed because the block is copy-on-write.
  63. * Do the copy and try again.
  64. */
  65. if(mode == OReadOnly || strcmp(vtGetError(), EBadRoot) != 0)
  66. goto Err;
  67. b = cacheLocalData(fs->cache, super.active, BtDir, RootTag, OReadWrite, 0);
  68. if(b == nil)
  69. goto Err;
  70. if(!(b->l.state&BsClosed) && b->l.epoch == fs->ehi){
  71. blockPut(b);
  72. goto Err;
  73. }
  74. b = blockCopy(b, RootTag, fs->ehi, fs->elo);
  75. if(b == nil)
  76. goto Err;
  77. localToGlobal(super.active, oscore);
  78. super.active = b->addr;
  79. bs = cacheLocal(fs->cache, PartSuper, 0, OReadWrite);
  80. if(bs == nil){
  81. blockPut(b);
  82. goto Err;
  83. }
  84. superPack(&super, bs->data);
  85. blockDependency(bs, b, 0, oscore, nil);
  86. blockDirty(bs);
  87. blockPut(bs);
  88. blockPut(b);
  89. fs->source = sourceRoot(fs, super.active, mode);
  90. if(fs->source == nil)
  91. goto Err;
  92. }
  93. fprint(2, "got fs source\n");
  94. vtRLock(fs->elk);
  95. fs->file = fileRoot(fs->source);
  96. vtRUnlock(fs->elk);
  97. if(fs->file == nil)
  98. goto Err;
  99. fprint(2, "got file root\n");
  100. if(mode == OReadWrite){
  101. fs->metaFlush = periodicAlloc(fsMetaFlush, fs, 1000);
  102. fs->snap = snapInit(fs);
  103. }
  104. return fs;
  105. Err:
  106. fsClose(fs);
  107. return nil;
  108. }
  109. void
  110. fsClose(Fs *fs)
  111. {
  112. vtRLock(fs->elk);
  113. periodicKill(fs->metaFlush);
  114. snapClose(fs->snap);
  115. if(fs->file){
  116. fileMetaFlush(fs->file, 0);
  117. if(!fileDecRef(fs->file))
  118. vtFatal("fsClose: files still in use: %r\n");
  119. }
  120. fs->file = nil;
  121. sourceClose(fs->source);
  122. cacheFree(fs->cache);
  123. if(fs->arch)
  124. archFree(fs->arch);
  125. vtRUnlock(fs->elk);
  126. vtLockFree(fs->elk);
  127. memset(fs, ~0, sizeof(Fs));
  128. vtMemFree(fs);
  129. }
  130. int
  131. fsRedial(Fs *fs, char *host)
  132. {
  133. if(!vtRedial(fs->z, host))
  134. return 0;
  135. if(!vtConnect(fs->z, 0))
  136. return 0;
  137. return 1;
  138. }
  139. File *
  140. fsGetRoot(Fs *fs)
  141. {
  142. return fileIncRef(fs->file);
  143. }
  144. int
  145. fsGetBlockSize(Fs *fs)
  146. {
  147. return fs->blockSize;
  148. }
  149. Block*
  150. superGet(Cache *c, Super* super)
  151. {
  152. Block *b;
  153. if((b = cacheLocal(c, PartSuper, 0, OReadWrite)) == nil){
  154. fprint(2, "superGet: cacheLocal failed: %R");
  155. return nil;
  156. }
  157. if(!superUnpack(super, b->data)){
  158. fprint(2, "superGet: superUnpack failed: %R");
  159. blockPut(b);
  160. return nil;
  161. }
  162. return b;
  163. }
  164. void
  165. superPut(Block* b, Super* super, int forceWrite)
  166. {
  167. superPack(super, b->data);
  168. blockDirty(b);
  169. if(forceWrite){
  170. while(!blockWrite(b)){
  171. /* BUG: what should really happen here? */
  172. fprint(2, "could not write super block; waiting 10 seconds\n");
  173. sleep(10*000);
  174. }
  175. while(b->iostate != BioClean && b->iostate != BioDirty){
  176. assert(b->iostate == BioWriting);
  177. vtSleep(b->ioready);
  178. }
  179. /*
  180. * it's okay that b might still be dirty.
  181. * that means it got written out but with an old root pointer,
  182. * but the other fields went out, and those are the ones
  183. * we really care about. (specifically, epochHigh; see fsSnapshot).
  184. */
  185. }
  186. blockPut(b);
  187. }
  188. /*
  189. * Prepare the directory to store a snapshot.
  190. * Temporary snapshots go into /snapshot/yyyy/mmdd/hhmm[.#]
  191. * Archival snapshots go into /archive/yyyy/mmdd[.#].
  192. *
  193. * TODO This should be rewritten to eliminate most of the duplication.
  194. */
  195. static File*
  196. fileOpenSnapshot(Fs *fs, int doarchive)
  197. {
  198. int n;
  199. char buf[30], *s;
  200. File *dir, *f;
  201. Tm now;
  202. if(doarchive){
  203. /*
  204. * a snapshot intended to be archived to venti.
  205. */
  206. dir = fileOpen(fs, "/archive");
  207. if(dir == nil)
  208. return nil;
  209. now = *localtime(time(0));
  210. /* yyyy */
  211. snprint(buf, sizeof(buf), "%d", now.year+1900);
  212. f = fileWalk(dir, buf);
  213. if(f == nil)
  214. f = fileCreate(dir, buf, ModeDir|0555, "adm");
  215. fileDecRef(dir);
  216. if(f == nil)
  217. return nil;
  218. dir = f;
  219. /* mmdd[#] */
  220. snprint(buf, sizeof(buf), "%02d%02d", now.mon+1, now.mday);
  221. s = buf+strlen(buf);
  222. for(n=0;; n++){
  223. if(n)
  224. seprint(s, buf+sizeof(buf), ".%d", n);
  225. f = fileWalk(dir, buf);
  226. if(f != nil){
  227. fileDecRef(f);
  228. continue;
  229. }
  230. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  231. break;
  232. }
  233. fileDecRef(dir);
  234. return f;
  235. }else{
  236. /*
  237. * Just a temporary snapshot
  238. * We'll use /snapshot/yyyy/mmdd/hhmm.
  239. * There may well be a better naming scheme.
  240. * (I'd have used hh:mm but ':' is reserved in Microsoft file systems.)
  241. */
  242. dir = fileOpen(fs, "/snapshot");
  243. if(dir == nil)
  244. return nil;
  245. /*
  246. * used to do /snapshot/#
  247. *
  248. for(n=0;; n++){
  249. if(n)
  250. seprint(s, buf+sizeof(buf), ".%d", n);
  251. f = fileWalk(dir, buf);
  252. if(f != nil){
  253. fileDecRef(f);
  254. continue;
  255. }
  256. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  257. break;
  258. }
  259. dir = fileOpen(fs, "/snapshot");
  260. if(dir == nil)
  261. return nil;
  262. snprint(buf, sizeof(buf), "%d", fs->ehi);
  263. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  264. fileDecRef(dir);
  265. return f;
  266. */
  267. now = *localtime(time(0));
  268. /* yyyy */
  269. snprint(buf, sizeof(buf), "%d", now.year+1900);
  270. f = fileWalk(dir, buf);
  271. if(f == nil)
  272. f = fileCreate(dir, buf, ModeDir|0555, "adm");
  273. fileDecRef(dir);
  274. if(f == nil)
  275. return nil;
  276. dir = f;
  277. /* mmdd */
  278. snprint(buf, sizeof(buf), "%02d%02d", now.mon+1, now.mday);
  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. /* hhmm[.#] */
  287. snprint(buf, sizeof buf, "%02d%02d", now.hour, now.min);
  288. s = buf+strlen(buf);
  289. for(n=0;; n++){
  290. if(n)
  291. seprint(s, buf+sizeof(buf), ".%d", n);
  292. f = fileWalk(dir, buf);
  293. if(f != nil){
  294. fileDecRef(f);
  295. continue;
  296. }
  297. f = fileCreate(dir, buf, ModeDir|ModeSnapshot|0555, "adm");
  298. break;
  299. }
  300. fileDecRef(dir);
  301. return f;
  302. }
  303. }
  304. int
  305. fsEpochLow(Fs *fs, u32int low)
  306. {
  307. Block *bs;
  308. Super super;
  309. vtLock(fs->elk);
  310. if(low > fs->ehi){
  311. vtSetError("bad low epoch (must be <= %ud)", fs->ehi);
  312. vtUnlock(fs->elk);
  313. return 0;
  314. }
  315. if((bs = superGet(fs->cache, &super)) == nil){
  316. vtUnlock(fs->elk);
  317. return 0;
  318. }
  319. super.epochLow = low;
  320. fs->elo = low;
  321. superPut(bs, &super, 1);
  322. vtUnlock(fs->elk);
  323. return 1;
  324. }
  325. static int
  326. bumpEpoch(Fs *fs, int doarchive)
  327. {
  328. uchar oscore[VtScoreSize];
  329. u32int oldaddr;
  330. Block *b, *bs;
  331. Entry e;
  332. Source *r;
  333. Super super;
  334. /*
  335. * Duplicate the root block.
  336. *
  337. * As a hint to flchk, the garbage collector,
  338. * and any (human) debuggers, store a pointer
  339. * to the old root block in entry 1 of the new root block.
  340. */
  341. r = fs->source;
  342. b = cacheGlobal(fs->cache, r->score, BtDir, RootTag, OReadOnly);
  343. if(b == nil)
  344. return 0;
  345. memset(&e, 0, sizeof e);
  346. e.flags = VtEntryActive | VtEntryLocal | VtEntryDir;
  347. memmove(e.score, b->score, VtScoreSize);
  348. e.tag = RootTag;
  349. e.snap = b->l.epoch;
  350. b = blockCopy(b, RootTag, fs->ehi+1, fs->elo);
  351. if(b == nil){
  352. fprint(2, "bumpEpoch: blockCopy: %R\n");
  353. return 0;
  354. }
  355. if(0) fprint(2, "snapshot root from %d to %d\n", oldaddr, b->addr);
  356. entryPack(&e, b->data, 1);
  357. blockDirty(b);
  358. /*
  359. * Update the superblock with the new root and epoch.
  360. */
  361. if((bs = superGet(fs->cache, &super)) == nil)
  362. return 0;
  363. fs->ehi++;
  364. memmove(r->score, b->score, VtScoreSize);
  365. r->epoch = fs->ehi;
  366. super.epochHigh = fs->ehi;
  367. oldaddr = super.active;
  368. super.active = b->addr;
  369. if(doarchive)
  370. super.next = oldaddr;
  371. /*
  372. * Record that the new super.active can't get written out until
  373. * the new b gets written out. Until then, use the old value.
  374. */
  375. localToGlobal(oldaddr, oscore);
  376. blockDependency(bs, b, 0, oscore, nil);
  377. blockPut(b);
  378. /*
  379. * We force the super block to disk so that super.epochHigh gets updated.
  380. * Otherwise, if we crash and come back, we might incorrectly treat as active
  381. * some of the blocks that making up the snapshot we just created.
  382. * Basically every block in the active file system and all the blocks in
  383. * the recently-created snapshot depend on the super block now.
  384. * Rather than record all those dependencies, we just force the block to disk.
  385. *
  386. * Note that blockWrite might actually (will probably) send a slightly outdated
  387. * super.active to disk. It will be the address of the most recent root that has
  388. * gone to disk.
  389. */
  390. superPut(bs, &super, 1);
  391. return 1;
  392. }
  393. int
  394. saveQid(Fs *fs)
  395. {
  396. Block *b;
  397. Super super;
  398. u64int qidMax;
  399. if((b = superGet(fs->cache, &super)) == nil)
  400. return 0;
  401. qidMax = super.qid;
  402. blockPut(b);
  403. if(!fileSetQidSpace(fs->file, 0, qidMax))
  404. return 0;
  405. return 1;
  406. }
  407. int
  408. fsSnapshot(Fs *fs, int doarchive)
  409. {
  410. File *src, *dst;
  411. assert(fs->mode == OReadWrite);
  412. dst = nil;
  413. /*
  414. * Freeze file system activity.
  415. */
  416. vtLock(fs->elk);
  417. /*
  418. * Get the root of the directory we're going to save.
  419. */
  420. src = fileOpen(fs, "/active");
  421. if(src == nil)
  422. goto Err;
  423. /*
  424. * It is important that we maintain the invariant that:
  425. * if both b and bb are marked as Active with epoch e
  426. * and b points at bb, then no other pointers to bb exist.
  427. *
  428. * The archiver uses this property to aggressively reclaim
  429. * such blocks once they have been stored on Venti, and
  430. * blockCleanup knows about this property as well.
  431. *
  432. * Let's say src->source is block sb, and src->msource is block
  433. * mb. Let's also say that block b holds the Entry structures for
  434. * both src->source and src->msource (their Entry structures might
  435. * be in different blocks, but the argument is the same).
  436. * That is, right now we have:
  437. *
  438. * b Active w/ epoch e, holds ptrs to sb and mb.
  439. * sb Active w/ epoch e.
  440. * mb Active w/ epoch e.
  441. *
  442. * With things as they are now, the invariant requires that
  443. * b holds the only pointers to sb and mb. We want to record
  444. * pointers to sb and mb in new Entries corresponding to dst,
  445. * which breaks the invariant. Thus we need to do something
  446. * about b. Specifically, we bump the file system's epoch and
  447. * then rewalk the path from the root down to and including b.
  448. * This will copy-on-write as we walk, so now the state will be:
  449. *
  450. * b Snap w/ epoch e, holds ptrs to sb and mb.
  451. * new-b Active w/ epoch e+1, holds ptrs to sb and mb.
  452. * sb Active w/ epoch e.
  453. * mb Active w/ epoch e.
  454. *
  455. * In this state, it's perfectly okay to add pointers to dst, which
  456. * will live in a block marked Active with epoch e+1.
  457. *
  458. * Of course, we need to make sure that the copied path makes
  459. * it out to disk before the new dst block; if the dst block goes out
  460. * first and then we crash, the invariant is violated. Rather than
  461. * deal with the dependencies, we just sync the file system to disk
  462. * right now.
  463. */
  464. if(!bumpEpoch(fs, 0) || !fileWalkSources(src))
  465. goto Err;
  466. /*
  467. * Sync to disk.
  468. */
  469. cacheFlush(fs->cache, 1);
  470. /*
  471. * Create the directory where we will store the copy of src.
  472. */
  473. dst = fileOpenSnapshot(fs, doarchive);
  474. if(dst == nil)
  475. goto Err;
  476. /*
  477. * Actually make the copy by setting dst's source and msource
  478. * to be src's.
  479. */
  480. if(!fileSnapshot(dst, src, fs->ehi-1, doarchive))
  481. goto Err;
  482. fileDecRef(src);
  483. fileDecRef(dst);
  484. /*
  485. * Make another copy of the file system. This one is for the
  486. * archiver, so that the file system we archive has the recently
  487. * added snapshot both in /active and in /archive/yyyy/mmdd[.#].
  488. */
  489. if(doarchive){
  490. if(!saveQid(fs))
  491. goto Err;
  492. if(!bumpEpoch(fs, 1))
  493. goto Err;
  494. }
  495. vtUnlock(fs->elk);
  496. /* BUG? can fs->arch fall out from under us here? */
  497. if(doarchive && fs->arch)
  498. archKick(fs->arch);
  499. return 1;
  500. Err:
  501. fprint(2, "fsSnapshot: %R\n");
  502. if(src)
  503. fileDecRef(src);
  504. if(dst)
  505. fileDecRef(dst);
  506. vtUnlock(fs->elk);
  507. return 0;
  508. }
  509. int
  510. fsVac(Fs *fs, char *name, uchar score[VtScoreSize])
  511. {
  512. int r;
  513. DirEntry de;
  514. Entry e, ee;
  515. File *f;
  516. vtRLock(fs->elk);
  517. f = fileOpen(fs, name);
  518. if(f == nil){
  519. vtRUnlock(fs->elk);
  520. return 0;
  521. }
  522. if(!fileGetSources(f, &e, &ee, 0) || !fileGetDir(f, &de)){
  523. fileDecRef(f);
  524. vtRUnlock(fs->elk);
  525. return 0;
  526. }
  527. fileDecRef(f);
  528. r = mkVac(fs->z, fs->blockSize, &e, &ee, &de, score);
  529. vtRUnlock(fs->elk);
  530. return r;
  531. }
  532. static int
  533. vtWriteBlock(VtSession *z, uchar *buf, uint n, uint type, uchar score[VtScoreSize])
  534. {
  535. if(!vtWrite(z, score, type, buf, n))
  536. return 0;
  537. if(!vtSha1Check(score, buf, n))
  538. return 0;
  539. return 1;
  540. }
  541. int
  542. mkVac(VtSession *z, uint blockSize, Entry *pe, Entry *pee, DirEntry *pde, uchar score[VtScoreSize])
  543. {
  544. uchar buf[8192];
  545. int i;
  546. uchar *p;
  547. uint n;
  548. DirEntry de;
  549. Entry e, ee, eee;
  550. MetaBlock mb;
  551. MetaEntry me;
  552. VtRoot root;
  553. e = *pe;
  554. ee = *pee;
  555. de = *pde;
  556. if(globalToLocal(e.score) != NilBlock
  557. || (ee.flags&VtEntryActive && globalToLocal(ee.score) != NilBlock)){
  558. vtSetError("can only vac paths already stored on venti");
  559. return 0;
  560. }
  561. /*
  562. * Build metadata source for root.
  563. */
  564. n = deSize(&de);
  565. if(n+MetaHeaderSize+MetaIndexSize > sizeof buf){
  566. vtSetError("DirEntry too big");
  567. return 0;
  568. }
  569. memset(buf, 0, sizeof buf);
  570. mbInit(&mb, buf, n+MetaHeaderSize+MetaIndexSize, 1);
  571. p = mbAlloc(&mb, n);
  572. if(p == nil)
  573. abort();
  574. mbSearch(&mb, de.elem, &i, &me);
  575. assert(me.p == nil);
  576. me.p = p;
  577. me.size = n;
  578. dePack(&de, &me);
  579. mbInsert(&mb, i, &me);
  580. mbPack(&mb);
  581. eee.size = n+MetaHeaderSize+MetaIndexSize;
  582. if(!vtWriteBlock(z, buf, eee.size, VtDataType, eee.score))
  583. return 0;
  584. eee.psize = 8192;
  585. eee.dsize = 8192;
  586. eee.depth = 0;
  587. eee.flags = VtEntryActive;
  588. /*
  589. * Build root source with three entries in it.
  590. */
  591. entryPack(&e, buf, 0);
  592. entryPack(&ee, buf, 1);
  593. entryPack(&eee, buf, 2);
  594. n = VtEntrySize*3;
  595. memset(&root, 0, sizeof root);
  596. if(!vtWriteBlock(z, buf, n, VtDirType, root.score))
  597. return 0;
  598. /*
  599. * Save root.
  600. */
  601. root.version = VtRootVersion;
  602. strcpy(root.type, "vac");
  603. strecpy(root.name, root.name+sizeof root.name, de.elem);
  604. root.blockSize = blockSize;
  605. vtRootPack(&root, buf);
  606. if(!vtWriteBlock(z, buf, VtRootSize, VtRootType, score))
  607. return 0;
  608. return 1;
  609. }
  610. int
  611. fsSync(Fs *fs)
  612. {
  613. vtLock(fs->elk);
  614. cacheFlush(fs->cache, 1);
  615. vtUnlock(fs->elk);
  616. return 1;
  617. }
  618. int
  619. fsNextQid(Fs *fs, u64int *qid)
  620. {
  621. Block *b;
  622. Super super;
  623. if((b = superGet(fs->cache, &super)) == nil)
  624. return 0;
  625. *qid = super.qid++;
  626. /*
  627. * It's okay if the super block doesn't go to disk immediately,
  628. * since fileMetaAlloc will record a dependency between the
  629. * block holding this qid and the super block. See file.c:/^fileMetaAlloc.
  630. */
  631. superPut(b, &super, 0);
  632. return 1;
  633. }
  634. static void
  635. fsMetaFlush(void *a)
  636. {
  637. Fs *fs = a;
  638. vtRLock(fs->elk);
  639. fileMetaFlush(fs->file, 1);
  640. vtRUnlock(fs->elk);
  641. cacheFlush(fs->cache, 0);
  642. }
  643. struct Snap
  644. {
  645. Fs *fs;
  646. Periodic *tick;
  647. VtLock *lk;
  648. uint snapMinutes;
  649. uint archMinute;
  650. u32int lastSnap;
  651. u32int lastArch;
  652. uint ignore;
  653. };
  654. static void
  655. snapEvent(void *v)
  656. {
  657. Snap *s;
  658. u32int now, min;
  659. Tm tm;
  660. s = v;
  661. now = time(0)/60;
  662. vtLock(s->lk);
  663. /*
  664. * Snapshots happen every snapMinutes minutes.
  665. * If we miss a snapshot (for example, because we
  666. * were down), we wait for the next one.
  667. */
  668. if(s->snapMinutes != ~0 && s->snapMinutes != 0
  669. && now%s->snapMinutes==0 && now != s->lastSnap){
  670. if(0)fprint(2, "snapshot %02d%02d\n", now/60, now%60);
  671. if(!fsSnapshot(s->fs, 0))
  672. fprint(2, "fsSnapshot snap: %R\n");
  673. s->lastSnap = now;
  674. }
  675. /*
  676. * Archival snapshots happen at archMinute.
  677. */
  678. tm = *localtime(now*60);
  679. min = tm.hour*60+tm.min;
  680. if(s->archMinute != ~0 && min == s->archMinute && now != s->lastArch){
  681. if(0)fprint(2, "archive %02d%02d\n", now/60, now%60);
  682. if(!fsSnapshot(s->fs, 1))
  683. fprint(2, "fsSnapshot arch: %R\n");
  684. s->lastArch = now;
  685. }
  686. vtUnlock(s->lk);
  687. }
  688. static Snap*
  689. snapInit(Fs *fs)
  690. {
  691. Snap *s;
  692. s = vtMemAllocZ(sizeof(Snap));
  693. s->fs = fs;
  694. s->tick = periodicAlloc(snapEvent, s, 10*1000);
  695. s->lk = vtLockAlloc();
  696. s->snapMinutes = -1;
  697. s->archMinute = -1;
  698. s->ignore = 5*2; /* wait five minutes for clock to stabilize */
  699. return s;
  700. }
  701. void
  702. snapGetTimes(Snap *s, u32int *arch, u32int *snap)
  703. {
  704. vtLock(s->lk);
  705. *snap = s->snapMinutes;
  706. *arch = s->archMinute;
  707. vtUnlock(s->lk);
  708. }
  709. void
  710. snapSetTimes(Snap *s, u32int arch, u32int snap)
  711. {
  712. vtLock(s->lk);
  713. s->snapMinutes = snap;
  714. s->archMinute = arch;
  715. vtUnlock(s->lk);
  716. }
  717. static void
  718. snapClose(Snap *s)
  719. {
  720. if(s == nil)
  721. return;
  722. periodicKill(s->tick);
  723. vtMemFree(s);
  724. }