source.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. #include "error.h"
  5. static int sizeToDepth(uvlong s, int psize, int dsize);
  6. static u32int tagGen(void);
  7. static Block *sourceLoad(Source *r, Entry *e);
  8. static Block *sourceLoadUnlocked(Source *r, Entry *e);
  9. static int sourceShrinkDepth(Source*, Block*, Entry*, int);
  10. static int sourceShrinkSize(Source*, Entry*, uvlong);
  11. static int sourceGrowDepth(Source*, Block*, Entry*, int);
  12. #define sourceIsLocked(r) ((r)->b != nil)
  13. static Source *
  14. sourceAlloc(Fs *fs, Block *b, Source *p, u32int offset, int mode)
  15. {
  16. Source *r;
  17. int epb;
  18. u32int epoch;
  19. Entry e;
  20. assert(p==nil || sourceIsLocked(p));
  21. if(p == nil){
  22. assert(offset == 0);
  23. epb = 1;
  24. }else
  25. epb = p->dsize / VtEntrySize;
  26. if(b->l.type != BtDir)
  27. goto Bad;
  28. /*
  29. * a non-active entry is the only thing that
  30. * can legitimately happen here. all the others
  31. * get prints.
  32. */
  33. if(!entryUnpack(&e, b->data, offset % epb)){
  34. fprint(2, "entryUnpack failed\n");
  35. goto Bad;
  36. }
  37. if(!(e.flags & VtEntryActive)){
  38. if(0)fprint(2, "not active\n");
  39. goto Bad;
  40. }
  41. if(e.psize < 256 || e.dsize < 256){
  42. fprint(2, "psize %ud dsize %ud\n", e.psize, e.dsize);
  43. goto Bad;
  44. }
  45. if(e.depth < sizeToDepth(e.size, e.psize, e.dsize)){
  46. fprint(2, "depth %ud size %llud psize %ud dsize %ud\n", e.depth, e.size, e.psize, e.dsize);
  47. goto Bad;
  48. }
  49. if((e.flags & VtEntryLocal) && e.tag == 0){
  50. fprint(2, "flags %#ux tag %#ux\n", e.flags, e.tag);
  51. goto Bad;
  52. }
  53. if(e.dsize > fs->blockSize || e.psize > fs->blockSize){
  54. fprint(2, "psize %ud dsize %ud blocksize %ud\n", e.psize, e.dsize, fs->blockSize);
  55. goto Bad;
  56. }
  57. epoch = b->l.epoch;
  58. if(mode == OReadWrite){
  59. if(e.snap != 0){
  60. vtSetError(ESnapRO);
  61. return nil;
  62. }
  63. }else if(e.snap != 0){
  64. if(e.snap < fs->elo){
  65. vtSetError(ESnapOld);
  66. return nil;
  67. }
  68. if(e.snap >= fs->ehi)
  69. goto Bad;
  70. epoch = e.snap;
  71. }
  72. r = vtMemAllocZ(sizeof(Source));
  73. r->fs = fs;
  74. r->mode = mode;
  75. r->dsize = e.dsize;
  76. r->gen = e.gen;
  77. r->dir = (e.flags & VtEntryDir) != 0;
  78. r->lk = vtLockAlloc();
  79. r->ref = 1;
  80. r->parent = p;
  81. if(p){
  82. vtLock(p->lk);
  83. assert(mode == OReadOnly || p->mode == OReadWrite);
  84. p->ref++;
  85. vtUnlock(p->lk);
  86. }
  87. r->epoch = epoch;
  88. //fprint(2, "sourceAlloc have %V be.%d fse.%d %s\n", b->score, b->l.epoch, r->fs->ehi, mode==OReadWrite ? "rw" : "ro");
  89. memmove(r->score, b->score, VtScoreSize);
  90. r->scoreEpoch = b->l.epoch;
  91. r->offset = offset;
  92. r->epb = epb;
  93. r->tag = b->l.tag;
  94. //fprint(2, "sourceAlloc: %p -> %V %d\n", r, r->score, r->offset);
  95. return r;
  96. Bad:
  97. vtSetError(EBadEntry);
  98. return nil;
  99. }
  100. Source *
  101. sourceRoot(Fs *fs, u32int addr, int mode)
  102. {
  103. Source *r;
  104. Block *b;
  105. b = cacheLocalData(fs->cache, addr, BtDir, RootTag, mode, 0);
  106. if(b == nil)
  107. return nil;
  108. if(mode == OReadWrite)
  109. if((b->l.state&BsClosed) || b->l.epoch != fs->ehi){
  110. fprint(2, "sourceRoot: fs->ehi = %ud, b->l = %L\n", fs->ehi, &b->l);
  111. blockPut(b);
  112. vtSetError(EBadRoot);
  113. return nil;
  114. }
  115. r = sourceAlloc(fs, b, nil, 0, mode);
  116. blockPut(b);
  117. return r;
  118. }
  119. Source *
  120. sourceOpen(Source *r, ulong offset, int mode)
  121. {
  122. ulong bn;
  123. Block *b;
  124. assert(sourceIsLocked(r));
  125. if(r->mode == OReadWrite)
  126. assert(r->epoch == r->b->l.epoch);
  127. if(!r->dir){
  128. vtSetError(ENotDir);
  129. return nil;
  130. }
  131. bn = offset/(r->dsize/VtEntrySize);
  132. b = sourceBlock(r, bn, mode);
  133. if(b == nil)
  134. return nil;
  135. r = sourceAlloc(r->fs, b, r, offset, mode);
  136. blockPut(b);
  137. return r;
  138. }
  139. Source *
  140. sourceCreate(Source *r, int dsize, int dir, u32int offset)
  141. {
  142. int i;
  143. Block *b;
  144. u32int bn, size;
  145. Entry e;
  146. int epb;
  147. int psize;
  148. Source *rr;
  149. assert(sourceIsLocked(r));
  150. if(!r->dir){
  151. vtSetError(ENotDir);
  152. return nil;
  153. }
  154. epb = r->dsize/VtEntrySize;
  155. psize = (dsize/VtScoreSize)*VtScoreSize;
  156. size = sourceGetDirSize(r);
  157. if(offset == 0){
  158. /*
  159. * look at a random block to see if we can find an empty entry
  160. */
  161. offset = lnrand(size+1);
  162. offset -= offset % epb;
  163. }
  164. /* try the given block and then try the last block */
  165. for(;;){
  166. bn = offset/epb;
  167. b = sourceBlock(r, bn, OReadWrite);
  168. if(b == nil)
  169. return nil;
  170. for(i=offset%r->epb; i<epb; i++){
  171. entryUnpack(&e, b->data, i);
  172. if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
  173. goto Found;
  174. }
  175. blockPut(b);
  176. if(offset == size){
  177. fprint(2, "sourceCreate: cannot happen\n");
  178. vtSetError("sourceCreate: cannot happen");
  179. return nil;
  180. }
  181. offset = size;
  182. }
  183. Found:
  184. /* found an entry - gen already set */
  185. e.psize = psize;
  186. e.dsize = dsize;
  187. e.flags = VtEntryActive;
  188. if(dir)
  189. e.flags |= VtEntryDir;
  190. e.depth = 0;
  191. e.size = 0;
  192. memmove(e.score, vtZeroScore, VtScoreSize);
  193. e.tag = 0;
  194. e.snap = 0;
  195. e.archive = 0;
  196. entryPack(&e, b->data, i);
  197. blockDirty(b);
  198. offset = bn*epb + i;
  199. if(offset+1 > size){
  200. if(!sourceSetDirSize(r, offset+1)){
  201. blockPut(b);
  202. return nil;
  203. }
  204. }
  205. rr = sourceAlloc(r->fs, b, r, offset, OReadWrite);
  206. blockPut(b);
  207. return rr;
  208. }
  209. static int
  210. sourceKill(Source *r, int doremove)
  211. {
  212. Entry e;
  213. Block *b;
  214. u32int addr;
  215. u32int tag;
  216. int type;
  217. assert(sourceIsLocked(r));
  218. b = sourceLoad(r, &e);
  219. if(b == nil)
  220. return 0;
  221. assert(b->l.epoch == r->fs->ehi);
  222. if(doremove==0 && e.size == 0){
  223. /* already truncated */
  224. blockPut(b);
  225. return 1;
  226. }
  227. /* remember info on link we are removing */
  228. addr = globalToLocal(e.score);
  229. type = entryType(&e);
  230. tag = e.tag;
  231. if(doremove){
  232. if(e.gen != ~0)
  233. e.gen++;
  234. e.dsize = 0;
  235. e.psize = 0;
  236. e.flags = 0;
  237. }else{
  238. e.flags &= ~VtEntryLocal;
  239. }
  240. e.depth = 0;
  241. e.size = 0;
  242. e.tag = 0;
  243. memmove(e.score, vtZeroScore, VtScoreSize);
  244. entryPack(&e, b->data, r->offset % r->epb);
  245. blockDirty(b);
  246. if(addr != NilBlock)
  247. blockRemoveLink(b, addr, type, tag);
  248. blockPut(b);
  249. if(doremove){
  250. sourceUnlock(r);
  251. sourceClose(r);
  252. }
  253. return 1;
  254. }
  255. int
  256. sourceRemove(Source *r)
  257. {
  258. return sourceKill(r, 1);
  259. }
  260. int
  261. sourceTruncate(Source *r)
  262. {
  263. return sourceKill(r, 0);
  264. }
  265. uvlong
  266. sourceGetSize(Source *r)
  267. {
  268. Entry e;
  269. Block *b;
  270. assert(sourceIsLocked(r));
  271. b = sourceLoad(r, &e);
  272. if(b == nil)
  273. return 0;
  274. blockPut(b);
  275. return e.size;
  276. }
  277. static int
  278. sourceShrinkSize(Source *r, Entry *e, uvlong size)
  279. {
  280. int i, type, ppb;
  281. uvlong ptrsz;
  282. u32int addr;
  283. uchar score[VtScoreSize];
  284. Block *b;
  285. type = entryType(e);
  286. b = cacheGlobal(r->fs->cache, e->score, type, e->tag, OReadWrite);
  287. if(b == nil)
  288. return 0;
  289. ptrsz = e->dsize;
  290. ppb = e->psize/VtScoreSize;
  291. for(i=0; i+1<e->depth; i++)
  292. ptrsz *= ppb;
  293. while(type&BtLevelMask){
  294. if(b->addr == NilBlock
  295. || (b->l.state&BsClosed)
  296. || b->l.epoch != r->fs->ehi){
  297. /* not worth copying the block just so we can zero some of it */
  298. blockPut(b);
  299. return 0;
  300. }
  301. /*
  302. * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
  303. */
  304. /* zero the pointers to unnecessary blocks */
  305. i = (size+ptrsz-1)/ptrsz;
  306. for(; i<ppb; i++){
  307. addr = globalToLocal(b->data+i*VtScoreSize);
  308. memmove(b->data+i*VtScoreSize, vtZeroScore, VtScoreSize);
  309. blockDirty(b);
  310. if(addr != NilBlock)
  311. blockRemoveLink(b, addr, type-1, e->tag);
  312. }
  313. /* recurse (go around again) on the partially necessary block */
  314. i = size/ptrsz;
  315. size = size%ptrsz;
  316. if(size == 0){
  317. blockPut(b);
  318. return 1;
  319. }
  320. ptrsz /= ppb;
  321. type--;
  322. memmove(score, b->data+i*VtScoreSize, VtScoreSize);
  323. blockPut(b);
  324. b = cacheGlobal(r->fs->cache, score, type, e->tag, OReadWrite);
  325. if(b == nil)
  326. return 0;
  327. }
  328. if(b->addr == NilBlock
  329. || (b->l.state&BsClosed)
  330. || b->l.epoch != r->fs->ehi){
  331. blockPut(b);
  332. return 0;
  333. }
  334. /*
  335. * No one ever truncates BtDir blocks.
  336. */
  337. if(type == BtData && e->dsize > size){
  338. memset(b->data+size, 0, e->dsize-size);
  339. blockDirty(b);
  340. }
  341. blockPut(b);
  342. return 1;
  343. }
  344. int
  345. sourceSetSize(Source *r, uvlong size)
  346. {
  347. int depth;
  348. Entry e;
  349. Block *b;
  350. assert(sourceIsLocked(r));
  351. if(size == 0)
  352. return sourceTruncate(r);
  353. if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){
  354. vtSetError(ETooBig);
  355. return 0;
  356. }
  357. b = sourceLoad(r, &e);
  358. if(b == nil)
  359. return 0;
  360. /* quick out */
  361. if(e.size == size){
  362. blockPut(b);
  363. return 1;
  364. }
  365. depth = sizeToDepth(size, e.psize, e.dsize);
  366. if(depth < e.depth){
  367. if(!sourceShrinkDepth(r, b, &e, depth)){
  368. blockPut(b);
  369. return 0;
  370. }
  371. }else if(depth > e.depth){
  372. if(!sourceGrowDepth(r, b, &e, depth)){
  373. blockPut(b);
  374. return 0;
  375. }
  376. }
  377. if(size < e.size)
  378. sourceShrinkSize(r, &e, size);
  379. e.size = size;
  380. entryPack(&e, b->data, r->offset % r->epb);
  381. blockDirty(b);
  382. blockPut(b);
  383. return 1;
  384. }
  385. int
  386. sourceSetDirSize(Source *r, ulong ds)
  387. {
  388. uvlong size;
  389. int epb;
  390. assert(sourceIsLocked(r));
  391. epb = r->dsize/VtEntrySize;
  392. size = (uvlong)r->dsize*(ds/epb);
  393. size += VtEntrySize*(ds%epb);
  394. return sourceSetSize(r, size);
  395. }
  396. ulong
  397. sourceGetDirSize(Source *r)
  398. {
  399. ulong ds;
  400. uvlong size;
  401. int epb;
  402. assert(sourceIsLocked(r));
  403. epb = r->dsize/VtEntrySize;
  404. size = sourceGetSize(r);
  405. ds = epb*(size/r->dsize);
  406. ds += (size%r->dsize)/VtEntrySize;
  407. return ds;
  408. }
  409. int
  410. sourceGetEntry(Source *r, Entry *e)
  411. {
  412. Block *b;
  413. assert(sourceIsLocked(r));
  414. b = sourceLoad(r, e);
  415. if(b == nil)
  416. return 0;
  417. blockPut(b);
  418. return 1;
  419. }
  420. static Block *
  421. blockWalk(Block *p, int index, int mode, Fs *fs, Entry *e)
  422. {
  423. Block *b;
  424. Cache *c;
  425. u32int addr;
  426. int type;
  427. uchar oscore[VtScoreSize];
  428. Entry oe;
  429. c = fs->cache;
  430. if((p->l.type & BtLevelMask) == 0){
  431. assert(p->l.type == BtDir);
  432. type = entryType(e);
  433. b = cacheGlobal(c, e->score, type, e->tag, mode);
  434. }else{
  435. type = p->l.type - 1;
  436. b = cacheGlobal(c, p->data + index*VtScoreSize, type, e->tag, mode);
  437. }
  438. if(b == nil || mode == OReadOnly)
  439. return b;
  440. assert(!(p->l.state&BsClosed) && p->l.epoch == fs->ehi);
  441. if(!(b->l.state&BsClosed) && b->l.epoch == fs->ehi)
  442. return b;
  443. oe = *e;
  444. /*
  445. * Copy on write.
  446. */
  447. if(e->tag == 0){
  448. assert(p->l.type == BtDir);
  449. e->tag = tagGen();
  450. e->flags |= VtEntryLocal;
  451. }
  452. addr = b->addr;
  453. b = blockCopy(b, e->tag, fs->ehi, fs->elo);
  454. if(b == nil)
  455. return nil;
  456. assert(b->l.epoch == fs->ehi);
  457. blockDirty(b);
  458. if(p->l.type == BtDir){
  459. memmove(e->score, b->score, VtScoreSize);
  460. entryPack(e, p->data, index);
  461. blockDependency(p, b, index, nil, &oe);
  462. }else{
  463. memmove(oscore, p->data+index*VtScoreSize, VtScoreSize);
  464. memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
  465. blockDependency(p, b, index, oscore, nil);
  466. }
  467. blockDirty(p);
  468. if(addr != NilBlock)
  469. blockRemoveLink(p, addr, type, e->tag);
  470. return b;
  471. }
  472. /*
  473. * Change the depth of the source r.
  474. * The entry e for r is contained in block p.
  475. */
  476. static int
  477. sourceGrowDepth(Source *r, Block *p, Entry *e, int depth)
  478. {
  479. Block *b, *bb;
  480. u32int tag;
  481. int type;
  482. Entry oe;
  483. assert(sourceIsLocked(r));
  484. assert(depth <= VtPointerDepth);
  485. type = entryType(e);
  486. b = cacheGlobal(r->fs->cache, e->score, type, e->tag, OReadWrite);
  487. if(b == nil)
  488. return 0;
  489. tag = e->tag;
  490. if(tag == 0)
  491. tag = tagGen();
  492. oe = *e;
  493. /*
  494. * Keep adding layers until we get to the right depth
  495. * or an error occurs.
  496. */
  497. while(e->depth < depth){
  498. bb = cacheAllocBlock(r->fs->cache, type+1, tag, r->fs->ehi, r->fs->elo);
  499. if(bb == nil)
  500. break;
  501. memmove(bb->data, b->score, VtScoreSize);
  502. memmove(e->score, bb->score, VtScoreSize);
  503. e->depth++;
  504. type++;
  505. e->tag = tag;
  506. e->flags |= VtEntryLocal;
  507. blockDependency(bb, b, 0, vtZeroScore, nil);
  508. blockPut(b);
  509. blockDirty(bb);
  510. b = bb;
  511. }
  512. entryPack(e, p->data, r->offset % r->epb);
  513. blockDependency(p, b, r->offset % r->epb, nil, &oe);
  514. blockPut(b);
  515. blockDirty(p);
  516. return e->depth == depth;
  517. }
  518. static int
  519. sourceShrinkDepth(Source *r, Block *p, Entry *e, int depth)
  520. {
  521. Block *b, *nb, *ob, *rb;
  522. u32int tag;
  523. int type, d;
  524. Entry oe;
  525. assert(sourceIsLocked(r));
  526. assert(depth <= VtPointerDepth);
  527. type = entryType(e);
  528. rb = cacheGlobal(r->fs->cache, e->score, type, e->tag, OReadWrite);
  529. if(rb == nil)
  530. return 0;
  531. tag = e->tag;
  532. if(tag == 0)
  533. tag = tagGen();
  534. /*
  535. * Walk down to the new root block.
  536. * We may stop early, but something is better than nothing.
  537. */
  538. oe = *e;
  539. ob = nil;
  540. b = rb;
  541. for(d=e->depth; d > depth; d--, type++){
  542. nb = cacheGlobal(r->fs->cache, b->data, type-1, tag, OReadWrite);
  543. if(nb == nil)
  544. break;
  545. if(ob!=nil && ob!=rb)
  546. blockPut(ob);
  547. ob = b;
  548. b = nb;
  549. }
  550. if(b == rb){
  551. blockPut(rb);
  552. return 0;
  553. }
  554. /*
  555. * Right now, e points at the root block rb, b is the new root block,
  556. * and ob points at b. To update:
  557. *
  558. * (i) change e to point at b
  559. * (ii) zero the pointer ob -> b
  560. * (iii) free the root block
  561. *
  562. * p (the block containing e) must be written before
  563. * anything else.
  564. */
  565. /* (i) */
  566. e->depth = d;
  567. memmove(e->score, b->score, VtScoreSize);
  568. entryPack(e, p->data, r->offset % r->epb);
  569. blockDependency(p, b, r->offset % r->epb, nil, &oe);
  570. blockDirty(p);
  571. /* (ii) */
  572. memmove(ob->data, vtZeroScore, VtScoreSize);
  573. blockDependency(ob, p, 0, b->score, nil);
  574. blockDirty(ob);
  575. /* (iii) */
  576. if(rb->addr != NilBlock)
  577. blockRemoveLink(p, rb->addr, rb->l.type, rb->l.tag);
  578. blockPut(rb);
  579. if(ob!=nil && ob!=rb)
  580. blockPut(ob);
  581. blockPut(b);
  582. return d == depth;
  583. }
  584. Block *
  585. sourceBlock(Source *r, ulong bn, int mode)
  586. {
  587. Block *b, *bb;
  588. int index[VtPointerDepth+1];
  589. Entry e;
  590. int i, np;
  591. int m;
  592. assert(sourceIsLocked(r));
  593. assert(bn != NilBlock);
  594. /* mode for intermediate block */
  595. m = mode;
  596. if(m == OOverWrite)
  597. m = OReadWrite;
  598. b = sourceLoad(r, &e);
  599. if(b == nil)
  600. return nil;
  601. np = e.psize/VtScoreSize;
  602. memset(index, 0, sizeof(index));
  603. for(i=0; bn > 0; i++){
  604. if(i >= VtPointerDepth){
  605. vtSetError(EBadAddr);
  606. goto Err;
  607. }
  608. index[i] = bn % np;
  609. bn /= np;
  610. }
  611. if(i > e.depth){
  612. if(mode == OReadOnly){
  613. vtSetError(EBadAddr);
  614. goto Err;
  615. }
  616. if(!sourceGrowDepth(r, b, &e, i))
  617. goto Err;
  618. }
  619. index[e.depth] = r->offset % r->epb;
  620. for(i=e.depth; i>=0; i--){
  621. bb = blockWalk(b, index[i], m, r->fs, &e);
  622. if(bb == nil)
  623. goto Err;
  624. blockPut(b);
  625. b = bb;
  626. }
  627. return b;
  628. Err:
  629. blockPut(b);
  630. return nil;
  631. }
  632. void
  633. sourceClose(Source *r)
  634. {
  635. if(r == nil)
  636. return;
  637. vtLock(r->lk);
  638. r->ref--;
  639. if(r->ref){
  640. vtUnlock(r->lk);
  641. return;
  642. }
  643. assert(r->ref == 0);
  644. vtUnlock(r->lk);
  645. if(r->parent)
  646. sourceClose(r->parent);
  647. vtLockFree(r->lk);
  648. memset(r, ~0, sizeof(*r));
  649. vtMemFree(r);
  650. }
  651. /*
  652. * Retrieve the block containing the entry for r.
  653. * If a snapshot has happened, we might need
  654. * to get a new copy of the block. We avoid this
  655. * in the common case by caching the score for
  656. * the block and the last epoch in which it was valid.
  657. *
  658. * We use r->mode to tell the difference between active
  659. * file system sources (OReadWrite) and sources for the
  660. * snapshot file system (OReadOnly).
  661. */
  662. static Block*
  663. sourceLoadBlock(Source *r, int mode)
  664. {
  665. u32int addr;
  666. Block *b;
  667. switch(r->mode){
  668. default:
  669. assert(0);
  670. case OReadWrite:
  671. assert(r->mode == OReadWrite);
  672. assert(r->epoch >= r->fs->elo);
  673. if(r->epoch == r->fs->ehi){
  674. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadWrite);
  675. assert(r->epoch == b->l.epoch);
  676. if(b == nil)
  677. return nil;
  678. return b;
  679. }
  680. assert(r->parent != nil);
  681. if(!sourceLock(r->parent, OReadWrite))
  682. return nil;
  683. b = sourceBlock(r->parent, r->offset/r->epb, OReadWrite);
  684. sourceUnlock(r->parent);
  685. if(b == nil)
  686. return nil;
  687. assert(b->l.epoch == r->fs->ehi);
  688. memmove(r->score, b->score, VtScoreSize);
  689. r->scoreEpoch = b->l.epoch;
  690. r->tag = b->l.tag;
  691. r->epoch = r->fs->ehi;
  692. return b;
  693. case OReadOnly:
  694. addr = globalToLocal(r->score);
  695. if(addr == NilBlock)
  696. return cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, mode);
  697. b = cacheLocalData(r->fs->cache, addr, BtDir, r->tag, mode, r->scoreEpoch);
  698. if(b)
  699. return b;
  700. /*
  701. * If it failed because the epochs don't match, the block has been
  702. * archived and reclaimed. Rewalk from the parent and get the
  703. * new pointer. This can't happen in the OReadWrite case
  704. * above because blocks in the current epoch don't get
  705. * reclaimed. The fact that we're OReadOnly means we're
  706. * a snapshot. (Or else the file system is read-only, but then
  707. * the archiver isn't going around deleting blocks.)
  708. */
  709. if(strcmp(vtGetError(), ELabelMismatch) == 0){
  710. if(!sourceLock(r->parent, OReadOnly))
  711. return nil;
  712. b = sourceBlock(r->parent, r->offset/r->epb, OReadOnly);
  713. sourceUnlock(r->parent);
  714. if(b){
  715. fprint(2, "sourceAlloc: lost %V found %V\n",
  716. r->score, b->score);
  717. memmove(r->score, b->score, VtScoreSize);
  718. r->scoreEpoch = b->l.epoch;
  719. return b;
  720. }
  721. }
  722. return nil;
  723. }
  724. }
  725. int
  726. sourceLock(Source *r, int mode)
  727. {
  728. Block *b;
  729. if(mode == -1)
  730. mode = r->mode;
  731. b = sourceLoadBlock(r, mode);
  732. if(b == nil)
  733. return 0;
  734. /*
  735. * The fact that we are holding b serves as the
  736. * lock entitling us to write to r->b.
  737. */
  738. assert(r->b == nil);
  739. r->b = b;
  740. if(r->mode == OReadWrite)
  741. assert(r->epoch == r->b->l.epoch);
  742. return 1;
  743. }
  744. /*
  745. * Lock two (usually sibling) sources. This needs special care
  746. * because the Entries for both sources might be in the same block.
  747. * We also try to lock blocks in left-to-right order within the tree.
  748. */
  749. int
  750. sourceLock2(Source *r, Source *rr, int mode)
  751. {
  752. Block *b, *bb;
  753. if(rr == nil)
  754. return sourceLock(r, mode);
  755. if(mode == -1)
  756. mode = r->mode;
  757. if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
  758. b = sourceLoadBlock(r, mode);
  759. if(b == nil)
  760. return 0;
  761. blockDupLock(b);
  762. bb = b;
  763. }else if(r->parent==rr->parent || r->offset > rr->offset){
  764. bb = sourceLoadBlock(rr, mode);
  765. b = sourceLoadBlock(r, mode);
  766. }else{
  767. b = sourceLoadBlock(r, mode);
  768. bb = sourceLoadBlock(rr, mode);
  769. }
  770. if(b == nil || bb == nil){
  771. if(b)
  772. blockPut(b);
  773. if(bb)
  774. blockPut(bb);
  775. return 0;
  776. }
  777. /*
  778. * The fact that we are holding b and bb serves
  779. * as the lock entitling us to write to r->b and rr->b.
  780. */
  781. r->b = b;
  782. rr->b = bb;
  783. return 1;
  784. }
  785. void
  786. sourceUnlock(Source *r)
  787. {
  788. Block *b;
  789. if(r->b == nil){
  790. fprint(2, "sourceUnlock: already unlocked\n");
  791. abort();
  792. }
  793. b = r->b;
  794. r->b = nil;
  795. blockPut(b);
  796. }
  797. static Block*
  798. sourceLoad(Source *r, Entry *e)
  799. {
  800. Block *b;
  801. assert(sourceIsLocked(r));
  802. b = r->b;
  803. if(!entryUnpack(e, b->data, r->offset % r->epb))
  804. return nil;
  805. if(e->gen != r->gen){
  806. vtSetError(ERemoved);
  807. return nil;
  808. }
  809. blockDupLock(b);
  810. return b;
  811. }
  812. static int
  813. sizeToDepth(uvlong s, int psize, int dsize)
  814. {
  815. int np;
  816. int d;
  817. /* determine pointer depth */
  818. np = psize/VtScoreSize;
  819. s = (s + dsize - 1)/dsize;
  820. for(d = 0; s > 1; d++)
  821. s = (s + np - 1)/np;
  822. return d;
  823. }
  824. static u32int
  825. tagGen(void)
  826. {
  827. u32int tag;
  828. for(;;){
  829. tag = lrand();
  830. if(tag >= UserTag)
  831. break;
  832. }
  833. return tag;
  834. }