fs.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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.state&BsClosed) && 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. blockPut(bs);
  95. fs->source = sourceRoot(fs, super.active, mode);
  96. if(fs->source == nil){
  97. vtSetError("sourceRoot: %R");
  98. goto Err;
  99. }
  100. }
  101. //fprint(2, "got fs source\n");
  102. vtRLock(fs->elk);
  103. fs->file = fileRoot(fs->source);
  104. vtRUnlock(fs->elk);
  105. if(fs->file == nil){
  106. vtSetError("fileRoot: %R");
  107. goto Err;
  108. }
  109. //fprint(2, "got file root\n");
  110. if(mode == OReadWrite){
  111. fs->metaFlush = periodicAlloc(fsMetaFlush, fs, 1000);
  112. fs->snap = snapInit(fs);
  113. }
  114. return fs;
  115. Err:
  116. fprint(2, "fsOpen error\n");
  117. fsClose(fs);
  118. return nil;
  119. }
  120. void
  121. fsClose(Fs *fs)
  122. {
  123. vtRLock(fs->elk);
  124. periodicKill(fs->metaFlush);
  125. snapClose(fs->snap);
  126. if(fs->file){
  127. fileMetaFlush(fs->file, 0);
  128. if(!fileDecRef(fs->file))
  129. vtFatal("fsClose: files still in use: %r\n");
  130. }
  131. fs->file = nil;
  132. sourceClose(fs->source);
  133. cacheFree(fs->cache);
  134. if(fs->arch)
  135. archFree(fs->arch);
  136. vtRUnlock(fs->elk);
  137. vtLockFree(fs->elk);
  138. memset(fs, ~0, sizeof(Fs));
  139. vtMemFree(fs);
  140. }
  141. int
  142. fsRedial(Fs *fs, char *host)
  143. {
  144. if(!vtRedial(fs->z, host))
  145. return 0;
  146. if(!vtConnect(fs->z, 0))
  147. return 0;
  148. return 1;
  149. }
  150. File *
  151. fsGetRoot(Fs *fs)
  152. {
  153. return fileIncRef(fs->file);
  154. }
  155. int
  156. fsGetBlockSize(Fs *fs)
  157. {
  158. return fs->blockSize;
  159. }
  160. Block*
  161. superGet(Cache *c, Super* super)
  162. {
  163. Block *b;
  164. if((b = cacheLocal(c, PartSuper, 0, OReadWrite)) == nil){
  165. fprint(2, "superGet: cacheLocal failed: %R");
  166. return nil;
  167. }
  168. if(!superUnpack(super, b->data)){
  169. fprint(2, "superGet: superUnpack failed: %R");
  170. blockPut(b);
  171. return nil;
  172. }
  173. return b;
  174. }
  175. void
  176. superPut(Block* b, Super* super, int forceWrite)
  177. {
  178. superPack(super, b->data);
  179. blockDirty(b);
  180. if(forceWrite){
  181. while(!blockWrite(b)){
  182. /* BUG: what should really happen here? */
  183. fprint(2, "could not write super block; waiting 10 seconds\n");
  184. sleep(10*000);
  185. }
  186. while(b->iostate != BioClean && b->iostate != BioDirty){
  187. assert(b->iostate == BioWriting);
  188. vtSleep(b->ioready);
  189. }
  190. /*
  191. * it's okay that b might still be dirty.
  192. * that means it got written out but with an old root pointer,
  193. * but the other fields went out, and those are the ones
  194. * we really care about. (specifically, epochHigh; see fsSnapshot).
  195. */
  196. }
  197. blockPut(b);
  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. superPut(bs, &super, 1);
  353. vtUnlock(fs->elk);
  354. return 1;
  355. }
  356. static int
  357. bumpEpoch(Fs *fs, int doarchive)
  358. {
  359. uchar oscore[VtScoreSize];
  360. u32int oldaddr;
  361. Block *b, *bs;
  362. Entry e;
  363. Source *r;
  364. Super super;
  365. /*
  366. * Duplicate the root block.
  367. *
  368. * As a hint to flchk, the garbage collector,
  369. * and any (human) debuggers, store a pointer
  370. * to the old root block in entry 1 of the new root block.
  371. */
  372. r = fs->source;
  373. b = cacheGlobal(fs->cache, r->score, BtDir, RootTag, OReadOnly);
  374. if(b == nil)
  375. return 0;
  376. memset(&e, 0, sizeof e);
  377. e.flags = VtEntryActive | VtEntryLocal | VtEntryDir;
  378. memmove(e.score, b->score, VtScoreSize);
  379. e.tag = RootTag;
  380. e.snap = b->l.epoch;
  381. b = blockCopy(b, RootTag, fs->ehi+1, fs->elo);
  382. if(b == nil){
  383. fprint(2, "bumpEpoch: blockCopy: %R\n");
  384. return 0;
  385. }
  386. if(0) fprint(2, "snapshot root from %d to %d\n", oldaddr, b->addr);
  387. entryPack(&e, b->data, 1);
  388. blockDirty(b);
  389. /*
  390. * Update the superblock with the new root and epoch.
  391. */
  392. if((bs = superGet(fs->cache, &super)) == nil)
  393. return 0;
  394. fs->ehi++;
  395. memmove(r->score, b->score, VtScoreSize);
  396. r->epoch = fs->ehi;
  397. super.epochHigh = fs->ehi;
  398. oldaddr = super.active;
  399. super.active = b->addr;
  400. if(doarchive)
  401. super.next = oldaddr;
  402. /*
  403. * Record that the new super.active can't get written out until
  404. * the new b gets written out. Until then, use the old value.
  405. */
  406. localToGlobal(oldaddr, oscore);
  407. blockDependency(bs, b, 0, oscore, nil);
  408. blockPut(b);
  409. /*
  410. * We force the super block to disk so that super.epochHigh gets updated.
  411. * Otherwise, if we crash and come back, we might incorrectly treat as active
  412. * some of the blocks that making up the snapshot we just created.
  413. * Basically every block in the active file system and all the blocks in
  414. * the recently-created snapshot depend on the super block now.
  415. * Rather than record all those dependencies, we just force the block to disk.
  416. *
  417. * Note that blockWrite might actually (will probably) send a slightly outdated
  418. * super.active to disk. It will be the address of the most recent root that has
  419. * gone to disk.
  420. */
  421. superPut(bs, &super, 1);
  422. return 1;
  423. }
  424. int
  425. saveQid(Fs *fs)
  426. {
  427. Block *b;
  428. Super super;
  429. u64int qidMax;
  430. if((b = superGet(fs->cache, &super)) == nil)
  431. return 0;
  432. qidMax = super.qid;
  433. blockPut(b);
  434. if(!fileSetQidSpace(fs->file, 0, qidMax))
  435. return 0;
  436. return 1;
  437. }
  438. int
  439. fsSnapshot(Fs *fs, char *srcpath, char *dstpath, int doarchive)
  440. {
  441. File *src, *dst;
  442. assert(fs->mode == OReadWrite);
  443. dst = nil;
  444. if(fs->halted){
  445. vtSetError("file system is halted");
  446. return 0;
  447. }
  448. /*
  449. * Freeze file system activity.
  450. */
  451. vtLock(fs->elk);
  452. /*
  453. * Get the root of the directory we're going to save.
  454. */
  455. if(srcpath == nil)
  456. srcpath = "/active";
  457. src = fileOpen(fs, srcpath);
  458. if(src == nil)
  459. goto Err;
  460. /*
  461. * It is important that we maintain the invariant that:
  462. * if both b and bb are marked as Active with epoch e
  463. * and b points at bb, then no other pointers to bb exist.
  464. *
  465. * The archiver uses this property to aggressively reclaim
  466. * such blocks once they have been stored on Venti, and
  467. * blockCleanup knows about this property as well.
  468. *
  469. * Let's say src->source is block sb, and src->msource is block
  470. * mb. Let's also say that block b holds the Entry structures for
  471. * both src->source and src->msource (their Entry structures might
  472. * be in different blocks, but the argument is the same).
  473. * That is, right now we have:
  474. *
  475. * b Active w/ epoch e, holds ptrs to sb and mb.
  476. * sb Active w/ epoch e.
  477. * mb Active w/ epoch e.
  478. *
  479. * With things as they are now, the invariant requires that
  480. * b holds the only pointers to sb and mb. We want to record
  481. * pointers to sb and mb in new Entries corresponding to dst,
  482. * which breaks the invariant. Thus we need to do something
  483. * about b. Specifically, we bump the file system's epoch and
  484. * then rewalk the path from the root down to and including b.
  485. * This will copy-on-write as we walk, so now the state will be:
  486. *
  487. * b Snap w/ epoch e, holds ptrs to sb and mb.
  488. * new-b Active w/ epoch e+1, holds ptrs to sb and mb.
  489. * sb Active w/ epoch e.
  490. * mb Active w/ epoch e.
  491. *
  492. * In this state, it's perfectly okay to add pointers to dst, which
  493. * will live in a block marked Active with epoch e+1.
  494. *
  495. * Of course, we need to make sure that the copied path makes
  496. * it out to disk before the new dst block; if the dst block goes out
  497. * first and then we crash, the invariant is violated. Rather than
  498. * deal with the dependencies, we just sync the file system to disk
  499. * right now.
  500. */
  501. if(!bumpEpoch(fs, 0) || !fileWalkSources(src))
  502. goto Err;
  503. /*
  504. * Sync to disk.
  505. */
  506. cacheFlush(fs->cache, 1);
  507. /*
  508. * Create the directory where we will store the copy of src.
  509. */
  510. dst = fileOpenSnapshot(fs, dstpath, doarchive);
  511. if(dst == nil)
  512. goto Err;
  513. /*
  514. * Actually make the copy by setting dst's source and msource
  515. * to be src's.
  516. */
  517. if(!fileSnapshot(dst, src, fs->ehi-1, doarchive))
  518. goto Err;
  519. fileDecRef(src);
  520. fileDecRef(dst);
  521. src = nil;
  522. dst = nil;
  523. /*
  524. * Make another copy of the file system. This one is for the
  525. * archiver, so that the file system we archive has the recently
  526. * added snapshot both in /active and in /archive/yyyy/mmdd[.#].
  527. */
  528. if(doarchive){
  529. if(!saveQid(fs))
  530. goto Err;
  531. if(!bumpEpoch(fs, 1))
  532. goto Err;
  533. }
  534. vtUnlock(fs->elk);
  535. /* BUG? can fs->arch fall out from under us here? */
  536. if(doarchive && fs->arch)
  537. archKick(fs->arch);
  538. return 1;
  539. Err:
  540. fprint(2, "fsSnapshot: %R\n");
  541. if(src)
  542. fileDecRef(src);
  543. if(dst)
  544. fileDecRef(dst);
  545. vtUnlock(fs->elk);
  546. return 0;
  547. }
  548. int
  549. fsVac(Fs *fs, char *name, uchar score[VtScoreSize])
  550. {
  551. int r;
  552. DirEntry de;
  553. Entry e, ee;
  554. File *f;
  555. vtRLock(fs->elk);
  556. f = fileOpen(fs, name);
  557. if(f == nil){
  558. vtRUnlock(fs->elk);
  559. return 0;
  560. }
  561. if(!fileGetSources(f, &e, &ee, 0) || !fileGetDir(f, &de)){
  562. fileDecRef(f);
  563. vtRUnlock(fs->elk);
  564. return 0;
  565. }
  566. fileDecRef(f);
  567. r = mkVac(fs->z, fs->blockSize, &e, &ee, &de, score);
  568. vtRUnlock(fs->elk);
  569. return r;
  570. }
  571. static int
  572. vtWriteBlock(VtSession *z, uchar *buf, uint n, uint type, uchar score[VtScoreSize])
  573. {
  574. if(!vtWrite(z, score, type, buf, n))
  575. return 0;
  576. if(!vtSha1Check(score, buf, n))
  577. return 0;
  578. return 1;
  579. }
  580. int
  581. mkVac(VtSession *z, uint blockSize, Entry *pe, Entry *pee, DirEntry *pde, uchar score[VtScoreSize])
  582. {
  583. uchar buf[8192];
  584. int i;
  585. uchar *p;
  586. uint n;
  587. DirEntry de;
  588. Entry e, ee, eee;
  589. MetaBlock mb;
  590. MetaEntry me;
  591. VtRoot root;
  592. e = *pe;
  593. ee = *pee;
  594. de = *pde;
  595. if(globalToLocal(e.score) != NilBlock
  596. || (ee.flags&VtEntryActive && globalToLocal(ee.score) != NilBlock)){
  597. vtSetError("can only vac paths already stored on venti");
  598. return 0;
  599. }
  600. /*
  601. * Build metadata source for root.
  602. */
  603. n = deSize(&de);
  604. if(n+MetaHeaderSize+MetaIndexSize > sizeof buf){
  605. vtSetError("DirEntry too big");
  606. return 0;
  607. }
  608. memset(buf, 0, sizeof buf);
  609. mbInit(&mb, buf, n+MetaHeaderSize+MetaIndexSize, 1);
  610. p = mbAlloc(&mb, n);
  611. if(p == nil)
  612. abort();
  613. mbSearch(&mb, de.elem, &i, &me);
  614. assert(me.p == nil);
  615. me.p = p;
  616. me.size = n;
  617. dePack(&de, &me);
  618. mbInsert(&mb, i, &me);
  619. mbPack(&mb);
  620. eee.size = n+MetaHeaderSize+MetaIndexSize;
  621. if(!vtWriteBlock(z, buf, eee.size, VtDataType, eee.score))
  622. return 0;
  623. eee.psize = 8192;
  624. eee.dsize = 8192;
  625. eee.depth = 0;
  626. eee.flags = VtEntryActive;
  627. /*
  628. * Build root source with three entries in it.
  629. */
  630. entryPack(&e, buf, 0);
  631. entryPack(&ee, buf, 1);
  632. entryPack(&eee, buf, 2);
  633. n = VtEntrySize*3;
  634. memset(&root, 0, sizeof root);
  635. if(!vtWriteBlock(z, buf, n, VtDirType, root.score))
  636. return 0;
  637. /*
  638. * Save root.
  639. */
  640. root.version = VtRootVersion;
  641. strecpy(root.type, root.type+sizeof root.type, "vac");
  642. strecpy(root.name, root.name+sizeof root.name, de.elem);
  643. root.blockSize = blockSize;
  644. vtRootPack(&root, buf);
  645. if(!vtWriteBlock(z, buf, VtRootSize, VtRootType, score))
  646. return 0;
  647. return 1;
  648. }
  649. int
  650. fsSync(Fs *fs)
  651. {
  652. vtLock(fs->elk);
  653. fileMetaFlush(fs->file, 1);
  654. cacheFlush(fs->cache, 1);
  655. vtUnlock(fs->elk);
  656. return 1;
  657. }
  658. int
  659. fsHalt(Fs *fs)
  660. {
  661. vtLock(fs->elk);
  662. fs->halted = 1;
  663. fileMetaFlush(fs->file, 1);
  664. cacheFlush(fs->cache, 1);
  665. return 1;
  666. }
  667. int
  668. fsUnhalt(Fs *fs)
  669. {
  670. if(!fs->halted)
  671. return 0;
  672. fs->halted = 0;
  673. vtUnlock(fs->elk);
  674. return 1;
  675. }
  676. int
  677. fsNextQid(Fs *fs, u64int *qid)
  678. {
  679. Block *b;
  680. Super super;
  681. if((b = superGet(fs->cache, &super)) == nil)
  682. return 0;
  683. *qid = super.qid++;
  684. /*
  685. * It's okay if the super block doesn't go to disk immediately,
  686. * since fileMetaAlloc will record a dependency between the
  687. * block holding this qid and the super block. See file.c:/^fileMetaAlloc.
  688. */
  689. superPut(b, &super, 0);
  690. return 1;
  691. }
  692. static void
  693. fsMetaFlush(void *a)
  694. {
  695. Fs *fs = a;
  696. vtRLock(fs->elk);
  697. fileMetaFlush(fs->file, 1);
  698. vtRUnlock(fs->elk);
  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, 0))
  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. s = v;
  876. now = time(0)/60;
  877. vtLock(s->lk);
  878. /*
  879. * Snapshots happen every snapMinutes minutes.
  880. * If we miss a snapshot (for example, because we
  881. * were down), we wait for the next one.
  882. */
  883. if(s->snapMinutes != ~0 && s->snapMinutes != 0
  884. && now%s->snapMinutes==0 && now != s->lastSnap){
  885. if(!fsSnapshot(s->fs, nil, nil, 0))
  886. fprint(2, "fsSnapshot snap: %R\n");
  887. s->lastSnap = now;
  888. }
  889. /*
  890. * Archival snapshots happen at archMinute.
  891. * If we miss an archive (for example, because we
  892. * were down), we do it as soon as possible.
  893. */
  894. tm = *localtime(now*60);
  895. min = tm.hour*60+tm.min;
  896. if(s->archMinute != ~0){
  897. need = 0;
  898. if(min == s->archMinute && now != s->lastArch)
  899. need = 1;
  900. if(s->lastArch == 0){
  901. s->lastArch = 1;
  902. if(fsNeedArch(s->fs, s->archMinute))
  903. need = 1;
  904. }
  905. if(need){
  906. fsSnapshot(s->fs, nil, nil, 1);
  907. s->lastArch = now;
  908. }
  909. }
  910. /*
  911. * Snapshot cleanup happens every snaplife or every day.
  912. */
  913. if(s->snapLife != ~0
  914. && (s->lastCleanup+s->snapLife < now || s->lastCleanup+24*60 < now)){
  915. fsSnapshotCleanup(s->fs, s->snapLife);
  916. s->lastCleanup = now;
  917. }
  918. vtUnlock(s->lk);
  919. }
  920. static Snap*
  921. snapInit(Fs *fs)
  922. {
  923. Snap *s;
  924. s = vtMemAllocZ(sizeof(Snap));
  925. s->fs = fs;
  926. s->tick = periodicAlloc(snapEvent, s, 10*1000);
  927. s->lk = vtLockAlloc();
  928. s->snapMinutes = -1;
  929. s->archMinute = -1;
  930. s->snapLife = -1;
  931. s->ignore = 5*2; /* wait five minutes for clock to stabilize */
  932. return s;
  933. }
  934. void
  935. snapGetTimes(Snap *s, u32int *arch, u32int *snap, u32int *snaplen)
  936. {
  937. vtLock(s->lk);
  938. *snap = s->snapMinutes;
  939. *arch = s->archMinute;
  940. *snaplen = s->snapLife;
  941. vtUnlock(s->lk);
  942. }
  943. void
  944. snapSetTimes(Snap *s, u32int arch, u32int snap, u32int snaplen)
  945. {
  946. vtLock(s->lk);
  947. s->snapMinutes = snap;
  948. s->archMinute = arch;
  949. s->snapLife = snaplen;
  950. vtUnlock(s->lk);
  951. }
  952. static void
  953. snapClose(Snap *s)
  954. {
  955. if(s == nil)
  956. return;
  957. periodicKill(s->tick);
  958. vtMemFree(s);
  959. }