file.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * Manage tree of VtFiles stored in the block cache.
  3. *
  4. * The single point of truth for the info about the VtFiles themselves
  5. * is the block data. Because of this, there is no explicit locking of
  6. * VtFile structures, and indeed there may be more than one VtFile
  7. * structure for a given Venti file. They synchronize through the
  8. * block cache.
  9. *
  10. * This is a bit simpler than fossil because there are no epochs
  11. * or tags or anything else. Just mutable local blocks and immutable
  12. * Venti blocks.
  13. */
  14. #include <u.h>
  15. #include <libc.h>
  16. #include <venti.h>
  17. #define MaxBlock (1UL<<31)
  18. static char ENotDir[] = "walk in non-directory";
  19. static char ETooBig[] = "file too big";
  20. /* static char EBadAddr[] = "bad address"; */
  21. static char ELabelMismatch[] = "label mismatch";
  22. static int sizetodepth(uvlong s, int psize, int dsize);
  23. static VtBlock *fileload(VtFile *r, VtEntry *e);
  24. static int shrinkdepth(VtFile*, VtBlock*, VtEntry*, int);
  25. static int shrinksize(VtFile*, VtEntry*, uvlong);
  26. static int growdepth(VtFile*, VtBlock*, VtEntry*, int);
  27. #define ISLOCKED(r) ((r)->b != nil)
  28. #define DEPTH(t) ((t)&VtTypeDepthMask)
  29. static VtFile *
  30. vtfilealloc(VtCache *c, VtBlock *b, VtFile *p, u32int offset, int mode)
  31. {
  32. int epb;
  33. u32int size;
  34. VtEntry e;
  35. VtFile *r;
  36. assert(p==nil || ISLOCKED(p));
  37. if(p == nil){
  38. assert(offset == 0);
  39. epb = 1;
  40. }else
  41. epb = p->dsize / VtEntrySize;
  42. if(b->type != VtDirType){
  43. werrstr("bad block type %#uo", b->type);
  44. return nil;
  45. }
  46. /*
  47. * a non-active entry is the only thing that
  48. * can legitimately happen here. all the others
  49. * get prints.
  50. */
  51. if(vtentryunpack(&e, b->data, offset % epb) < 0){
  52. fprint(2, "vtentryunpack failed: %r (%.*H)\n", VtEntrySize, b->data+VtEntrySize*(offset%epb));
  53. return nil;
  54. }
  55. if(!(e.flags & VtEntryActive)){
  56. werrstr("entry not active");
  57. return nil;
  58. }
  59. if(DEPTH(e.type) < sizetodepth(e.size, e.psize, e.dsize)){
  60. fprint(2, "depth %ud size %llud psize %ud dsize %ud\n",
  61. DEPTH(e.type), e.size, e.psize, e.dsize);
  62. werrstr("bad depth");
  63. return nil;
  64. }
  65. size = vtcacheblocksize(c);
  66. if(e.dsize > size || e.psize > size){
  67. werrstr("block sizes %ud, %ud bigger than cache block size %ud",
  68. e.psize, e.dsize, size);
  69. return nil;
  70. }
  71. r = vtmallocz(sizeof(VtFile));
  72. r->c = c;
  73. r->mode = mode;
  74. r->dsize = e.dsize;
  75. r->psize = e.psize;
  76. r->gen = e.gen;
  77. r->dir = (e.type & VtTypeBaseMask) == VtDirType;
  78. r->ref = 1;
  79. r->parent = p;
  80. if(p){
  81. qlock(&p->lk);
  82. assert(mode == VtOREAD || p->mode == VtORDWR);
  83. p->ref++;
  84. qunlock(&p->lk);
  85. }else{
  86. assert(b->addr != NilBlock);
  87. r->local = 1;
  88. }
  89. memmove(r->score, b->score, VtScoreSize);
  90. r->offset = offset;
  91. r->epb = epb;
  92. return r;
  93. }
  94. VtFile *
  95. vtfileroot(VtCache *c, u32int addr, int mode)
  96. {
  97. VtFile *r;
  98. VtBlock *b;
  99. b = vtcachelocal(c, addr, VtDirType);
  100. if(b == nil)
  101. return nil;
  102. r = vtfilealloc(c, b, nil, 0, mode);
  103. vtblockput(b);
  104. return r;
  105. }
  106. VtFile*
  107. vtfileopenroot(VtCache *c, VtEntry *e)
  108. {
  109. VtBlock *b;
  110. VtFile *f;
  111. b = vtcacheallocblock(c, VtDirType);
  112. if(b == nil)
  113. return nil;
  114. vtentrypack(e, b->data, 0);
  115. f = vtfilealloc(c, b, nil, 0, VtORDWR);
  116. vtblockput(b);
  117. return f;
  118. }
  119. VtFile *
  120. vtfilecreateroot(VtCache *c, int psize, int dsize, int type)
  121. {
  122. VtEntry e;
  123. memset(&e, 0, sizeof e);
  124. e.flags = VtEntryActive;
  125. e.psize = psize;
  126. e.dsize = dsize;
  127. e.type = type;
  128. memmove(e.score, vtzeroscore, VtScoreSize);
  129. return vtfileopenroot(c, &e);
  130. }
  131. VtFile *
  132. vtfileopen(VtFile *r, u32int offset, int mode)
  133. {
  134. ulong bn;
  135. VtBlock *b;
  136. assert(ISLOCKED(r));
  137. if(!r->dir){
  138. werrstr(ENotDir);
  139. return nil;
  140. }
  141. bn = offset/(r->dsize/VtEntrySize);
  142. b = vtfileblock(r, bn, mode);
  143. if(b == nil)
  144. return nil;
  145. r = vtfilealloc(r->c, b, r, offset, mode);
  146. vtblockput(b);
  147. return r;
  148. }
  149. VtFile*
  150. vtfilecreate(VtFile *r, int psize, int dsize, int type)
  151. {
  152. return _vtfilecreate(r, -1, psize, dsize, type);
  153. }
  154. VtFile*
  155. _vtfilecreate(VtFile *r, int o, int psize, int dsize, int type)
  156. {
  157. int i;
  158. VtBlock *b;
  159. u32int bn, size;
  160. VtEntry e;
  161. int epb;
  162. VtFile *rr;
  163. u32int offset;
  164. assert(ISLOCKED(r));
  165. assert(psize <= VtMaxLumpSize);
  166. assert(dsize <= VtMaxLumpSize);
  167. assert(type == VtDirType || type == VtDataType);
  168. if(!r->dir){
  169. werrstr(ENotDir);
  170. return nil;
  171. }
  172. epb = r->dsize/VtEntrySize;
  173. size = vtfilegetdirsize(r);
  174. /*
  175. * look at a random block to see if we can find an empty entry
  176. */
  177. if(o == -1){
  178. offset = lnrand(size+1);
  179. offset -= offset % epb;
  180. }else
  181. offset = o;
  182. /* try the given block and then try the last block */
  183. for(;;){
  184. bn = offset/epb;
  185. b = vtfileblock(r, bn, VtORDWR);
  186. if(b == nil)
  187. return nil;
  188. for(i=offset%r->epb; i<epb; i++){
  189. if(vtentryunpack(&e, b->data, i) < 0)
  190. continue;
  191. if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
  192. goto Found;
  193. }
  194. vtblockput(b);
  195. if(offset == size){
  196. fprint(2, "vtfilecreate: cannot happen\n");
  197. werrstr("vtfilecreate: cannot happen");
  198. return nil;
  199. }
  200. offset = size;
  201. }
  202. Found:
  203. /* found an entry - gen already set */
  204. e.psize = psize;
  205. e.dsize = dsize;
  206. e.flags = VtEntryActive;
  207. e.type = type;
  208. e.size = 0;
  209. memmove(e.score, vtzeroscore, VtScoreSize);
  210. vtentrypack(&e, b->data, i);
  211. offset = bn*epb + i;
  212. if(offset+1 > size){
  213. if(vtfilesetdirsize(r, offset+1) < 0){
  214. vtblockput(b);
  215. return nil;
  216. }
  217. }
  218. rr = vtfilealloc(r->c, b, r, offset, VtORDWR);
  219. vtblockput(b);
  220. return rr;
  221. }
  222. static int
  223. vtfilekill(VtFile *r, int doremove)
  224. {
  225. VtEntry e;
  226. VtBlock *b;
  227. assert(ISLOCKED(r));
  228. b = fileload(r, &e);
  229. if(b == nil)
  230. return -1;
  231. if(doremove==0 && e.size == 0){
  232. /* already truncated */
  233. vtblockput(b);
  234. return 0;
  235. }
  236. if(doremove){
  237. if(e.gen != ~0)
  238. e.gen++;
  239. e.dsize = 0;
  240. e.psize = 0;
  241. e.flags = 0;
  242. }else
  243. e.flags &= ~VtEntryLocal;
  244. e.type = 0;
  245. e.size = 0;
  246. memmove(e.score, vtzeroscore, VtScoreSize);
  247. vtentrypack(&e, b->data, r->offset % r->epb);
  248. vtblockput(b);
  249. if(doremove){
  250. vtfileunlock(r);
  251. vtfileclose(r);
  252. }
  253. return 0;
  254. }
  255. int
  256. vtfileremove(VtFile *r)
  257. {
  258. return vtfilekill(r, 1);
  259. }
  260. int
  261. vtfiletruncate(VtFile *r)
  262. {
  263. return vtfilekill(r, 0);
  264. }
  265. uvlong
  266. vtfilegetsize(VtFile *r)
  267. {
  268. VtEntry e;
  269. VtBlock *b;
  270. assert(ISLOCKED(r));
  271. b = fileload(r, &e);
  272. if(b == nil)
  273. return ~(uvlong)0;
  274. vtblockput(b);
  275. return e.size;
  276. }
  277. static int
  278. shrinksize(VtFile *r, VtEntry *e, uvlong size)
  279. {
  280. int i, depth, type, isdir, ppb;
  281. uvlong ptrsz;
  282. uchar score[VtScoreSize];
  283. VtBlock *b;
  284. b = vtcacheglobal(r->c, e->score, e->type);
  285. if(b == nil)
  286. return -1;
  287. ptrsz = e->dsize;
  288. ppb = e->psize/VtScoreSize;
  289. type = e->type;
  290. depth = DEPTH(type);
  291. for(i=0; i+1<depth; i++)
  292. ptrsz *= ppb;
  293. isdir = r->dir;
  294. while(depth > 0){
  295. if(b->addr == NilBlock){
  296. /* not worth copying the block just so we can zero some of it */
  297. vtblockput(b);
  298. return -1;
  299. }
  300. /*
  301. * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
  302. */
  303. /* zero the pointers to unnecessary blocks */
  304. i = (size+ptrsz-1)/ptrsz;
  305. for(; i<ppb; i++)
  306. memmove(b->data+i*VtScoreSize, vtzeroscore, VtScoreSize);
  307. /* recurse (go around again) on the partially necessary block */
  308. i = size/ptrsz;
  309. size = size%ptrsz;
  310. if(size == 0){
  311. vtblockput(b);
  312. return 0;
  313. }
  314. ptrsz /= ppb;
  315. type--;
  316. memmove(score, b->data+i*VtScoreSize, VtScoreSize);
  317. vtblockput(b);
  318. b = vtcacheglobal(r->c, score, type);
  319. if(b == nil)
  320. return -1;
  321. }
  322. if(b->addr == NilBlock){
  323. vtblockput(b);
  324. return -1;
  325. }
  326. /*
  327. * No one ever truncates BtDir blocks.
  328. */
  329. if(depth==0 && !isdir && e->dsize > size)
  330. memset(b->data+size, 0, e->dsize-size);
  331. vtblockput(b);
  332. return 0;
  333. }
  334. int
  335. vtfilesetsize(VtFile *r, u64int size)
  336. {
  337. int depth, edepth;
  338. VtEntry e;
  339. VtBlock *b;
  340. assert(ISLOCKED(r));
  341. if(size == 0)
  342. return vtfiletruncate(r);
  343. if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){
  344. werrstr(ETooBig);
  345. return -1;
  346. }
  347. b = fileload(r, &e);
  348. if(b == nil)
  349. return -1;
  350. /* quick out */
  351. if(e.size == size){
  352. vtblockput(b);
  353. return 0;
  354. }
  355. depth = sizetodepth(size, e.psize, e.dsize);
  356. edepth = DEPTH(e.type);
  357. if(depth < edepth){
  358. if(shrinkdepth(r, b, &e, depth) < 0){
  359. vtblockput(b);
  360. return -1;
  361. }
  362. }else if(depth > edepth){
  363. if(growdepth(r, b, &e, depth) < 0){
  364. vtblockput(b);
  365. return -1;
  366. }
  367. }
  368. if(size < e.size)
  369. shrinksize(r, &e, size);
  370. e.size = size;
  371. vtentrypack(&e, b->data, r->offset % r->epb);
  372. vtblockput(b);
  373. return 0;
  374. }
  375. int
  376. vtfilesetdirsize(VtFile *r, u32int ds)
  377. {
  378. uvlong size;
  379. int epb;
  380. assert(ISLOCKED(r));
  381. epb = r->dsize/VtEntrySize;
  382. size = (uvlong)r->dsize*(ds/epb);
  383. size += VtEntrySize*(ds%epb);
  384. return vtfilesetsize(r, size);
  385. }
  386. u32int
  387. vtfilegetdirsize(VtFile *r)
  388. {
  389. ulong ds;
  390. uvlong size;
  391. int epb;
  392. assert(ISLOCKED(r));
  393. epb = r->dsize/VtEntrySize;
  394. size = vtfilegetsize(r);
  395. ds = epb*(size/r->dsize);
  396. ds += (size%r->dsize)/VtEntrySize;
  397. return ds;
  398. }
  399. int
  400. vtfilegetentry(VtFile *r, VtEntry *e)
  401. {
  402. VtBlock *b;
  403. assert(ISLOCKED(r));
  404. b = fileload(r, e);
  405. if(b == nil)
  406. return -1;
  407. vtblockput(b);
  408. return 0;
  409. }
  410. int
  411. vtfilesetentry(VtFile *r, VtEntry *e)
  412. {
  413. VtBlock *b;
  414. VtEntry ee;
  415. assert(ISLOCKED(r));
  416. b = fileload(r, &ee);
  417. if(b == nil)
  418. return -1;
  419. vtentrypack(e, b->data, r->offset % r->epb);
  420. vtblockput(b);
  421. return 0;
  422. }
  423. static VtBlock *
  424. blockwalk(VtBlock *p, int index, VtCache *c, int mode, VtEntry *e)
  425. {
  426. VtBlock *b;
  427. int type;
  428. uchar *score;
  429. VtEntry oe;
  430. switch(p->type){
  431. case VtDataType:
  432. assert(0);
  433. case VtDirType:
  434. type = e->type;
  435. score = e->score;
  436. break;
  437. default:
  438. type = p->type - 1;
  439. score = p->data+index*VtScoreSize;
  440. break;
  441. }
  442. /*print("walk from %V/%d ty %d to %V ty %d\n", p->score, index, p->type, score, type); */
  443. if(mode == VtOWRITE && vtglobaltolocal(score) == NilBlock){
  444. b = vtcacheallocblock(c, type);
  445. if(b)
  446. goto HaveCopy;
  447. }else
  448. b = vtcacheglobal(c, score, type);
  449. if(b == nil || mode == VtOREAD)
  450. return b;
  451. if(vtglobaltolocal(b->score) != NilBlock)
  452. return b;
  453. oe = *e;
  454. /*
  455. * Copy on write.
  456. */
  457. e->flags |= VtEntryLocal;
  458. b = vtblockcopy(b/*, e->tag, fs->ehi, fs->elo*/);
  459. if(b == nil)
  460. return nil;
  461. HaveCopy:
  462. if(p->type == VtDirType){
  463. memmove(e->score, b->score, VtScoreSize);
  464. vtentrypack(e, p->data, index);
  465. }else{
  466. memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
  467. }
  468. return b;
  469. }
  470. /*
  471. * Change the depth of the VtFile r.
  472. * The entry e for r is contained in block p.
  473. */
  474. static int
  475. growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
  476. {
  477. VtBlock *b, *bb;
  478. VtEntry oe;
  479. assert(ISLOCKED(r));
  480. assert(depth <= VtPointerDepth);
  481. b = vtcacheglobal(r->c, e->score, e->type);
  482. if(b == nil)
  483. return -1;
  484. oe = *e;
  485. /*
  486. * Keep adding layers until we get to the right depth
  487. * or an error occurs.
  488. */
  489. while(DEPTH(e->type) < depth){
  490. bb = vtcacheallocblock(r->c, e->type+1);
  491. if(bb == nil)
  492. break;
  493. memmove(bb->data, b->score, VtScoreSize);
  494. memmove(e->score, bb->score, VtScoreSize);
  495. e->type++;
  496. e->flags |= VtEntryLocal;
  497. vtblockput(b);
  498. b = bb;
  499. }
  500. vtentrypack(e, p->data, r->offset % r->epb);
  501. vtblockput(b);
  502. if(DEPTH(e->type) == depth)
  503. return 0;
  504. return -1;
  505. }
  506. static int
  507. shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
  508. {
  509. VtBlock *b, *nb, *ob, *rb;
  510. VtEntry oe;
  511. assert(ISLOCKED(r));
  512. assert(depth <= VtPointerDepth);
  513. rb = vtcacheglobal(r->c, e->score, e->type);
  514. if(rb == nil)
  515. return 0;
  516. /*
  517. * Walk down to the new root block.
  518. * We may stop early, but something is better than nothing.
  519. */
  520. oe = *e;
  521. ob = nil;
  522. b = rb;
  523. for(; DEPTH(e->type) > depth; e->type--){
  524. nb = vtcacheglobal(r->c, b->data, e->type-1);
  525. if(nb == nil)
  526. break;
  527. if(ob!=nil && ob!=rb)
  528. vtblockput(ob);
  529. ob = b;
  530. b = nb;
  531. }
  532. if(b == rb){
  533. vtblockput(rb);
  534. return 0;
  535. }
  536. /*
  537. * Right now, e points at the root block rb, b is the new root block,
  538. * and ob points at b. To update:
  539. *
  540. * (i) change e to point at b
  541. * (ii) zero the pointer ob -> b
  542. * (iii) free the root block
  543. *
  544. * p (the block containing e) must be written before
  545. * anything else.
  546. */
  547. /* (i) */
  548. memmove(e->score, b->score, VtScoreSize);
  549. vtentrypack(e, p->data, r->offset % r->epb);
  550. /* (ii) */
  551. memmove(ob->data, vtzeroscore, VtScoreSize);
  552. /* (iii) */
  553. vtblockput(rb);
  554. if(ob!=nil && ob!=rb)
  555. vtblockput(ob);
  556. vtblockput(b);
  557. if(DEPTH(e->type) == depth)
  558. return 0;
  559. return -1;
  560. }
  561. static int
  562. mkindices(VtEntry *e, u32int bn, int *index)
  563. {
  564. int i, np;
  565. memset(index, 0, (VtPointerDepth+1)*sizeof(int));
  566. np = e->psize/VtScoreSize;
  567. for(i=0; bn > 0; i++){
  568. if(i >= VtPointerDepth){
  569. werrstr("bad address 0x%lux", (ulong)bn);
  570. return -1;
  571. }
  572. index[i] = bn % np;
  573. bn /= np;
  574. }
  575. return i;
  576. }
  577. VtBlock *
  578. vtfileblock(VtFile *r, u32int bn, int mode)
  579. {
  580. VtBlock *b, *bb;
  581. int index[VtPointerDepth+1];
  582. VtEntry e;
  583. int i;
  584. int m;
  585. assert(ISLOCKED(r));
  586. assert(bn != NilBlock);
  587. b = fileload(r, &e);
  588. if(b == nil)
  589. return nil;
  590. i = mkindices(&e, bn, index);
  591. if(i < 0)
  592. goto Err;
  593. if(i > DEPTH(e.type)){
  594. if(mode == VtOREAD){
  595. werrstr("bad address 0x%lux", (ulong)bn);
  596. goto Err;
  597. }
  598. index[i] = 0;
  599. if(growdepth(r, b, &e, i) < 0)
  600. goto Err;
  601. }
  602. assert(b->type == VtDirType);
  603. index[DEPTH(e.type)] = r->offset % r->epb;
  604. /* mode for intermediate block */
  605. m = mode;
  606. if(m == VtOWRITE)
  607. m = VtORDWR;
  608. for(i=DEPTH(e.type); i>=0; i--){
  609. bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e);
  610. if(bb == nil)
  611. goto Err;
  612. vtblockput(b);
  613. b = bb;
  614. }
  615. b->pc = getcallerpc(&r);
  616. return b;
  617. Err:
  618. vtblockput(b);
  619. return nil;
  620. }
  621. int
  622. vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize])
  623. {
  624. VtBlock *b, *bb;
  625. int index[VtPointerDepth+1];
  626. VtEntry e;
  627. int i;
  628. assert(ISLOCKED(r));
  629. assert(bn != NilBlock);
  630. b = fileload(r, &e);
  631. if(b == nil)
  632. return -1;
  633. i = mkindices(&e, bn, index);
  634. if(i < 0){
  635. vtblockput(b);
  636. return -1;
  637. }
  638. if(i > DEPTH(e.type)){
  639. memmove(score, vtzeroscore, VtScoreSize);
  640. vtblockput(b);
  641. return 0;
  642. }
  643. index[DEPTH(e.type)] = r->offset % r->epb;
  644. for(i=DEPTH(e.type); i>=1; i--){
  645. bb = blockwalk(b, index[i], r->c, VtOREAD, &e);
  646. if(bb == nil)
  647. goto Err;
  648. vtblockput(b);
  649. b = bb;
  650. if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0)
  651. break;
  652. }
  653. memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize);
  654. vtblockput(b);
  655. return 0;
  656. Err:
  657. vtblockput(b);
  658. return -1;
  659. }
  660. void
  661. vtfileincref(VtFile *r)
  662. {
  663. qlock(&r->lk);
  664. r->ref++;
  665. qunlock(&r->lk);
  666. }
  667. void
  668. vtfileclose(VtFile *r)
  669. {
  670. if(r == nil)
  671. return;
  672. qlock(&r->lk);
  673. r->ref--;
  674. if(r->ref){
  675. qunlock(&r->lk);
  676. return;
  677. }
  678. assert(r->ref == 0);
  679. qunlock(&r->lk);
  680. if(r->parent)
  681. vtfileclose(r->parent);
  682. memset(r, ~0, sizeof(*r));
  683. vtfree(r);
  684. }
  685. /*
  686. * Retrieve the block containing the entry for r.
  687. * If a snapshot has happened, we might need
  688. * to get a new copy of the block. We avoid this
  689. * in the common case by caching the score for
  690. * the block and the last epoch in which it was valid.
  691. *
  692. * We use r->mode to tell the difference between active
  693. * file system VtFiles (VtORDWR) and VtFiles for the
  694. * snapshot file system (VtOREAD).
  695. */
  696. static VtBlock*
  697. fileloadblock(VtFile *r, int mode)
  698. {
  699. char e[ERRMAX];
  700. u32int addr;
  701. VtBlock *b;
  702. switch(r->mode){
  703. default:
  704. assert(0);
  705. case VtORDWR:
  706. assert(r->mode == VtORDWR);
  707. if(r->local == 1){
  708. b = vtcacheglobal(r->c, r->score, VtDirType);
  709. if(b == nil)
  710. return nil;
  711. b->pc = getcallerpc(&r);
  712. return b;
  713. }
  714. assert(r->parent != nil);
  715. if(vtfilelock(r->parent, VtORDWR) < 0)
  716. return nil;
  717. b = vtfileblock(r->parent, r->offset/r->epb, VtORDWR);
  718. vtfileunlock(r->parent);
  719. if(b == nil)
  720. return nil;
  721. memmove(r->score, b->score, VtScoreSize);
  722. r->local = 1;
  723. return b;
  724. case VtOREAD:
  725. if(mode == VtORDWR){
  726. werrstr("read/write lock of read-only file");
  727. return nil;
  728. }
  729. addr = vtglobaltolocal(r->score);
  730. if(addr == NilBlock)
  731. return vtcacheglobal(r->c, r->score, VtDirType);
  732. b = vtcachelocal(r->c, addr, VtDirType);
  733. if(b)
  734. return b;
  735. /*
  736. * If it failed because the epochs don't match, the block has been
  737. * archived and reclaimed. Rewalk from the parent and get the
  738. * new pointer. This can't happen in the VtORDWR case
  739. * above because blocks in the current epoch don't get
  740. * reclaimed. The fact that we're VtOREAD means we're
  741. * a snapshot. (Or else the file system is read-only, but then
  742. * the archiver isn't going around deleting blocks.)
  743. */
  744. rerrstr(e, sizeof e);
  745. if(strcmp(e, ELabelMismatch) == 0){
  746. if(vtfilelock(r->parent, VtOREAD) < 0)
  747. return nil;
  748. b = vtfileblock(r->parent, r->offset/r->epb, VtOREAD);
  749. vtfileunlock(r->parent);
  750. if(b){
  751. fprint(2, "vtfilealloc: lost %V found %V\n",
  752. r->score, b->score);
  753. memmove(r->score, b->score, VtScoreSize);
  754. return b;
  755. }
  756. }
  757. return nil;
  758. }
  759. }
  760. int
  761. vtfilelock(VtFile *r, int mode)
  762. {
  763. VtBlock *b;
  764. if(mode == -1)
  765. mode = r->mode;
  766. b = fileloadblock(r, mode);
  767. if(b == nil)
  768. return -1;
  769. /*
  770. * The fact that we are holding b serves as the
  771. * lock entitling us to write to r->b.
  772. */
  773. assert(r->b == nil);
  774. r->b = b;
  775. b->pc = getcallerpc(&r);
  776. return 0;
  777. }
  778. /*
  779. * Lock two (usually sibling) VtFiles. This needs special care
  780. * because the Entries for both vtFiles might be in the same block.
  781. * We also try to lock blocks in left-to-right order within the tree.
  782. */
  783. int
  784. vtfilelock2(VtFile *r, VtFile *rr, int mode)
  785. {
  786. VtBlock *b, *bb;
  787. if(rr == nil)
  788. return vtfilelock(r, mode);
  789. if(mode == -1)
  790. mode = r->mode;
  791. if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
  792. b = fileloadblock(r, mode);
  793. if(b == nil)
  794. return -1;
  795. vtblockduplock(b);
  796. bb = b;
  797. }else if(r->parent==rr->parent || r->offset > rr->offset){
  798. bb = fileloadblock(rr, mode);
  799. b = fileloadblock(r, mode);
  800. }else{
  801. b = fileloadblock(r, mode);
  802. bb = fileloadblock(rr, mode);
  803. }
  804. if(b == nil || bb == nil){
  805. if(b)
  806. vtblockput(b);
  807. if(bb)
  808. vtblockput(bb);
  809. return -1;
  810. }
  811. /*
  812. * The fact that we are holding b and bb serves
  813. * as the lock entitling us to write to r->b and rr->b.
  814. */
  815. r->b = b;
  816. rr->b = bb;
  817. b->pc = getcallerpc(&r);
  818. bb->pc = getcallerpc(&r);
  819. return 0;
  820. }
  821. void
  822. vtfileunlock(VtFile *r)
  823. {
  824. VtBlock *b;
  825. if(r->b == nil){
  826. fprint(2, "vtfileunlock: already unlocked\n");
  827. abort();
  828. }
  829. b = r->b;
  830. r->b = nil;
  831. vtblockput(b);
  832. }
  833. static VtBlock*
  834. fileload(VtFile *r, VtEntry *e)
  835. {
  836. VtBlock *b;
  837. assert(ISLOCKED(r));
  838. b = r->b;
  839. if(vtentryunpack(e, b->data, r->offset % r->epb) < 0)
  840. return nil;
  841. vtblockduplock(b);
  842. return b;
  843. }
  844. static int
  845. sizetodepth(uvlong s, int psize, int dsize)
  846. {
  847. int np;
  848. int d;
  849. /* determine pointer depth */
  850. np = psize/VtScoreSize;
  851. s = (s + dsize - 1)/dsize;
  852. for(d = 0; s > 1; d++)
  853. s = (s + np - 1)/np;
  854. return d;
  855. }
  856. long
  857. vtfileread(VtFile *f, void *data, long count, vlong offset)
  858. {
  859. int frag;
  860. VtBlock *b;
  861. VtEntry e;
  862. assert(ISLOCKED(f));
  863. vtfilegetentry(f, &e);
  864. if(count == 0)
  865. return 0;
  866. if(count < 0 || offset < 0){
  867. werrstr("vtfileread: bad offset or count");
  868. return -1;
  869. }
  870. if(offset >= e.size)
  871. return 0;
  872. if(offset+count > e.size)
  873. count = e.size - offset;
  874. frag = offset % e.dsize;
  875. if(frag+count > e.dsize)
  876. count = e.dsize - frag;
  877. b = vtfileblock(f, offset/e.dsize, VtOREAD);
  878. if(b == nil)
  879. return -1;
  880. memmove(data, b->data+frag, count);
  881. vtblockput(b);
  882. return count;
  883. }
  884. static long
  885. filewrite1(VtFile *f, void *data, long count, vlong offset)
  886. {
  887. int frag, m;
  888. VtBlock *b;
  889. VtEntry e;
  890. vtfilegetentry(f, &e);
  891. if(count < 0 || offset < 0){
  892. werrstr("vtfilewrite: bad offset or count");
  893. return -1;
  894. }
  895. frag = offset % e.dsize;
  896. if(frag+count > e.dsize)
  897. count = e.dsize - frag;
  898. m = VtORDWR;
  899. if(frag == 0 && count == e.dsize)
  900. m = VtOWRITE;
  901. b = vtfileblock(f, offset/e.dsize, m);
  902. if(b == nil)
  903. return -1;
  904. memmove(b->data+frag, data, count);
  905. if(offset+count > e.size){
  906. vtfilegetentry(f, &e);
  907. e.size = offset+count;
  908. vtfilesetentry(f, &e);
  909. }
  910. vtblockput(b);
  911. return count;
  912. }
  913. long
  914. vtfilewrite(VtFile *f, void *data, long count, vlong offset)
  915. {
  916. long tot, m;
  917. assert(ISLOCKED(f));
  918. tot = 0;
  919. m = 0;
  920. while(tot < count){
  921. m = filewrite1(f, (char*)data+tot, count-tot, offset+tot);
  922. if(m <= 0)
  923. break;
  924. tot += m;
  925. }
  926. if(tot==0)
  927. return m;
  928. return tot;
  929. }
  930. static int
  931. flushblock(VtCache *c, VtBlock *bb, uchar score[VtScoreSize], int ppb, int epb,
  932. int type)
  933. {
  934. u32int addr;
  935. VtBlock *b;
  936. VtEntry e;
  937. int i;
  938. addr = vtglobaltolocal(score);
  939. if(addr == NilBlock)
  940. return 0;
  941. if(bb){
  942. b = bb;
  943. if(memcmp(b->score, score, VtScoreSize) != 0)
  944. abort();
  945. }else
  946. if((b = vtcachelocal(c, addr, type)) == nil)
  947. return -1;
  948. switch(type){
  949. case VtDataType:
  950. break;
  951. case VtDirType:
  952. for(i=0; i<epb; i++){
  953. if(vtentryunpack(&e, b->data, i) < 0)
  954. goto Err;
  955. if(flushblock(c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
  956. e.type) < 0)
  957. goto Err;
  958. }
  959. break;
  960. default: /* VtPointerTypeX */
  961. for(i=0; i<ppb; i++){
  962. if(flushblock(c, nil, b->data+VtScoreSize*i, ppb, epb, type-1) < 0)
  963. goto Err;
  964. }
  965. break;
  966. }
  967. if(vtblockwrite(b) < 0)
  968. goto Err;
  969. memmove(score, b->score, VtScoreSize);
  970. if(b != bb)
  971. vtblockput(b);
  972. return 0;
  973. Err:
  974. if(b != bb)
  975. vtblockput(b);
  976. return -1;
  977. }
  978. int
  979. vtfileflush(VtFile *f)
  980. {
  981. int ret;
  982. VtBlock *b;
  983. VtEntry e;
  984. assert(ISLOCKED(f));
  985. b = fileload(f, &e);
  986. if(!(e.flags&VtEntryLocal)){
  987. vtblockput(b);
  988. return 0;
  989. }
  990. ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
  991. e.type);
  992. if(ret < 0){
  993. vtblockput(b);
  994. return -1;
  995. }
  996. vtentrypack(&e, b->data, f->offset % f->epb);
  997. vtblockput(b);
  998. return 0;
  999. }
  1000. int
  1001. vtfileflushbefore(VtFile *r, u64int offset)
  1002. {
  1003. VtBlock *b, *bb;
  1004. VtEntry e;
  1005. int i, base, depth, ppb, epb, doflush;
  1006. int index[VtPointerDepth+1], j, ret;
  1007. VtBlock *bi[VtPointerDepth+2];
  1008. uchar *score;
  1009. assert(ISLOCKED(r));
  1010. if(offset == 0)
  1011. return 0;
  1012. b = fileload(r, &e);
  1013. if(b == nil)
  1014. return -1;
  1015. /*
  1016. * compute path through tree for the last written byte and the next one.
  1017. */
  1018. ret = -1;
  1019. memset(bi, 0, sizeof bi);
  1020. depth = DEPTH(e.type);
  1021. bi[depth+1] = b;
  1022. i = mkindices(&e, (offset-1)/e.dsize, index);
  1023. if(i < 0)
  1024. goto Err;
  1025. if(i > depth)
  1026. goto Err;
  1027. ppb = e.psize / VtScoreSize;
  1028. epb = e.dsize / VtEntrySize;
  1029. /*
  1030. * load the blocks along the last written byte
  1031. */
  1032. index[depth] = r->offset % r->epb;
  1033. for(i=depth; i>=0; i--){
  1034. bb = blockwalk(b, index[i], r->c, VtORDWR, &e);
  1035. if(bb == nil)
  1036. goto Err;
  1037. bi[i] = bb;
  1038. b = bb;
  1039. }
  1040. ret = 0;
  1041. /*
  1042. * walk up the path from leaf to root, flushing anything that
  1043. * has been finished.
  1044. */
  1045. base = e.type&~VtTypeDepthMask;
  1046. for(i=0; i<=depth; i++){
  1047. doflush = 0;
  1048. if(i == 0){
  1049. /* leaf: data or dir block */
  1050. if(offset%e.dsize == 0)
  1051. doflush = 1;
  1052. }else{
  1053. /*
  1054. * interior node: pointer blocks.
  1055. * specifically, b = bi[i] is a block whose index[i-1]'th entry
  1056. * points at bi[i-1].
  1057. */
  1058. b = bi[i];
  1059. /*
  1060. * the index entries up to but not including index[i-1] point at
  1061. * finished blocks, so flush them for sure.
  1062. */
  1063. for(j=0; j<index[i-1]; j++)
  1064. if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0)
  1065. goto Err;
  1066. /*
  1067. * if index[i-1] is the last entry in the block and is global
  1068. * (i.e. the kid is flushed), then we can flush this block.
  1069. */
  1070. if(j==ppb-1 && vtglobaltolocal(b->data+j*VtScoreSize)==NilBlock)
  1071. doflush = 1;
  1072. }
  1073. if(doflush){
  1074. if(i == depth)
  1075. score = e.score;
  1076. else
  1077. score = bi[i+1]->data+index[i]*VtScoreSize;
  1078. if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0)
  1079. goto Err;
  1080. }
  1081. }
  1082. Err:
  1083. /* top: entry. do this always so that the score is up-to-date */
  1084. vtentrypack(&e, bi[depth+1]->data, index[depth]);
  1085. for(i=0; i<nelem(bi); i++)
  1086. if(bi[i])
  1087. vtblockput(bi[i]);
  1088. return ret;
  1089. }