file.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. #include "error.h"
  5. /*
  6. * locking order is upwards. A thread can hold the lock for a File
  7. * and then acquire the lock of its parent
  8. */
  9. struct File {
  10. Fs *fs; /* immutable */
  11. /* meta data for file: protected by the lk in the parent */
  12. int ref; /* holds this data structure up */
  13. int partial; /* file was never really open */
  14. int removed; /* file has been removed */
  15. int dirty; /* dir is dirty with respect to meta data in block */
  16. u32int boff; /* block offset within msource for this file's meta data */
  17. DirEntry dir; /* meta data for this file */
  18. File *up; /* parent file */
  19. File *next; /* sibling */
  20. /* data for file */
  21. VtLock *lk; /* lock for the following */
  22. Source *source;
  23. Source *msource; /* for directories: meta data for children */
  24. File *down; /* children */
  25. int mode;
  26. int issnapshot;
  27. };
  28. static int fileMetaFlush2(File*, char*);
  29. static u32int fileMetaAlloc(File*, DirEntry*, u32int);
  30. static int fileRLock(File*);
  31. static void fileRUnlock(File*);
  32. static int fileLock(File*);
  33. static void fileUnlock(File*);
  34. static void fileMetaLock(File*);
  35. static void fileMetaUnlock(File*);
  36. static void fileRAccess(File*);
  37. static void fileWAccess(File*, char*);
  38. static File *
  39. fileAlloc(Fs *fs)
  40. {
  41. File *f;
  42. f = vtMemAllocZ(sizeof(File));
  43. f->lk = vtLockAlloc();
  44. f->ref = 1;
  45. f->fs = fs;
  46. f->boff = NilBlock;
  47. f->mode = fs->mode;
  48. return f;
  49. }
  50. static void
  51. fileFree(File *f)
  52. {
  53. sourceClose(f->source);
  54. vtLockFree(f->lk);
  55. sourceClose(f->msource);
  56. deCleanup(&f->dir);
  57. memset(f, ~0, sizeof(File));
  58. vtMemFree(f);
  59. }
  60. /*
  61. * the file is locked already
  62. * f->msource is unlocked
  63. */
  64. static File *
  65. dirLookup(File *f, char *elem)
  66. {
  67. int i;
  68. MetaBlock mb;
  69. MetaEntry me;
  70. Block *b;
  71. Source *meta;
  72. File *ff;
  73. u32int bo, nb;
  74. meta = f->msource;
  75. b = nil;
  76. if(!sourceLock(meta, -1))
  77. return nil;
  78. nb = (sourceGetSize(meta)+meta->dsize-1)/meta->dsize;
  79. for(bo=0; bo<nb; bo++){
  80. b = sourceBlock(meta, bo, OReadOnly);
  81. if(b == nil)
  82. goto Err;
  83. if(!mbUnpack(&mb, b->data, meta->dsize))
  84. goto Err;
  85. if(mbSearch(&mb, elem, &i, &me)){
  86. ff = fileAlloc(f->fs);
  87. if(!deUnpack(&ff->dir, &me)){
  88. fileFree(ff);
  89. goto Err;
  90. }
  91. sourceUnlock(meta);
  92. blockPut(b);
  93. ff->boff = bo;
  94. ff->mode = f->mode;
  95. ff->issnapshot = f->issnapshot;
  96. return ff;
  97. }
  98. blockPut(b);
  99. b = nil;
  100. }
  101. vtSetError(ENoFile);
  102. /* fall through */
  103. Err:
  104. sourceUnlock(meta);
  105. blockPut(b);
  106. return nil;
  107. }
  108. File *
  109. fileRoot(Source *r)
  110. {
  111. Block *b;
  112. Source *r0, *r1, *r2;
  113. MetaBlock mb;
  114. MetaEntry me;
  115. File *root, *mr;
  116. Fs *fs;
  117. b = nil;
  118. root = nil;
  119. mr = nil;
  120. r1 = nil;
  121. r2 = nil;
  122. fs = r->fs;
  123. if(!sourceLock(r, -1))
  124. return nil;
  125. r0 = sourceOpen(r, 0, fs->mode, 0);
  126. if(r0 == nil)
  127. goto Err;
  128. r1 = sourceOpen(r, 1, fs->mode, 0);
  129. if(r1 == nil)
  130. goto Err;
  131. r2 = sourceOpen(r, 2, fs->mode, 0);
  132. if(r2 == nil)
  133. goto Err;
  134. mr = fileAlloc(fs);
  135. mr->msource = r2;
  136. r2 = nil;
  137. root = fileAlloc(fs);
  138. root->boff = 0;
  139. root->up = mr;
  140. root->source = r0;
  141. r0 = nil;
  142. root->msource = r1;
  143. r1 = nil;
  144. mr->down = root;
  145. if(!sourceLock(mr->msource, -1))
  146. goto Err;
  147. b = sourceBlock(mr->msource, 0, OReadOnly);
  148. sourceUnlock(mr->msource);
  149. if(b == nil)
  150. goto Err;
  151. if(!mbUnpack(&mb, b->data, mr->msource->dsize))
  152. goto Err;
  153. meUnpack(&me, &mb, 0);
  154. if(!deUnpack(&root->dir, &me))
  155. goto Err;
  156. blockPut(b);
  157. sourceUnlock(r);
  158. fileRAccess(root);
  159. return root;
  160. Err:
  161. blockPut(b);
  162. if(r0)
  163. sourceClose(r0);
  164. if(r1)
  165. sourceClose(r1);
  166. if(r2)
  167. sourceClose(r2);
  168. if(mr)
  169. fileFree(mr);
  170. if(root)
  171. fileFree(root);
  172. sourceUnlock(r);
  173. return nil;
  174. }
  175. static Source *
  176. fileOpenSource(File *f, u32int offset, u32int gen, int dir, uint mode, int issnapshot)
  177. {
  178. Source *r;
  179. if(!sourceLock(f->source, mode))
  180. return nil;
  181. r = sourceOpen(f->source, offset, mode, issnapshot);
  182. sourceUnlock(f->source);
  183. if(r == nil)
  184. return nil;
  185. if(r->gen != gen){
  186. vtSetError(ERemoved);
  187. goto Err;
  188. }
  189. if(r->dir != dir && r->mode != -1){
  190. fprint(2, "fileOpenSource: dir mismatch %d %d\n", r->dir, dir);
  191. vtSetError(EBadMeta);
  192. goto Err;
  193. }
  194. return r;
  195. Err:
  196. sourceClose(r);
  197. return nil;
  198. }
  199. File *
  200. _fileWalk(File *f, char *elem, int partial)
  201. {
  202. File *ff;
  203. fileRAccess(f);
  204. if(elem[0] == 0){
  205. vtSetError(EBadPath);
  206. return nil;
  207. }
  208. if(!fileIsDir(f)){
  209. vtSetError(ENotDir);
  210. return nil;
  211. }
  212. if(strcmp(elem, ".") == 0){
  213. return fileIncRef(f);
  214. }
  215. if(strcmp(elem, "..") == 0){
  216. if(fileIsRoot(f))
  217. return fileIncRef(f);
  218. return fileIncRef(f->up);
  219. }
  220. if(!fileLock(f))
  221. return nil;
  222. for(ff = f->down; ff; ff=ff->next){
  223. if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
  224. ff->ref++;
  225. goto Exit;
  226. }
  227. }
  228. ff = dirLookup(f, elem);
  229. if(ff == nil)
  230. goto Err;
  231. if(ff->dir.mode & ModeSnapshot){
  232. ff->mode = OReadOnly;
  233. ff->issnapshot = 1;
  234. }
  235. if(partial){
  236. /*
  237. * Do nothing. We're opening this file only so we can clri it.
  238. * Usually the sources can't be opened, hence we won't even bother.
  239. * Be VERY careful with the returned file. If you hand it to a routine
  240. * expecting ff->source and/or ff->msource to be non-nil, we're
  241. * likely to dereference nil. FileClri should be the only routine
  242. * setting partial.
  243. */
  244. ff->partial = 1;
  245. }else if(ff->dir.mode & ModeDir){
  246. ff->source = fileOpenSource(f, ff->dir.entry, ff->dir.gen, 1, ff->mode, ff->issnapshot);
  247. ff->msource = fileOpenSource(f, ff->dir.mentry, ff->dir.mgen, 0, ff->mode, ff->issnapshot);
  248. if(ff->source == nil || ff->msource == nil)
  249. goto Err;
  250. }else{
  251. ff->source = fileOpenSource(f, ff->dir.entry, ff->dir.gen, 0, ff->mode, ff->issnapshot);
  252. if(ff->source == nil)
  253. goto Err;
  254. }
  255. /* link in and up parent ref count */
  256. ff->next = f->down;
  257. f->down = ff;
  258. ff->up = f;
  259. fileIncRef(f);
  260. Exit:
  261. fileUnlock(f);
  262. return ff;
  263. Err:
  264. fileUnlock(f);
  265. if(ff != nil)
  266. fileDecRef(ff);
  267. return nil;
  268. }
  269. File *
  270. fileWalk(File *f, char *elem)
  271. {
  272. return _fileWalk(f, elem, 0);
  273. }
  274. File *
  275. _fileOpen(Fs *fs, char *path, int partial)
  276. {
  277. File *f, *ff;
  278. char *p, elem[VtMaxStringSize], *opath;
  279. int n;
  280. f = fs->file;
  281. fileIncRef(f);
  282. opath = path;
  283. while(*path != 0){
  284. for(p = path; *p && *p != '/'; p++)
  285. ;
  286. n = p - path;
  287. if(n > 0){
  288. if(n > VtMaxStringSize){
  289. vtSetError("%s: element too long", EBadPath);
  290. goto Err;
  291. }
  292. memmove(elem, path, n);
  293. elem[n] = 0;
  294. ff = _fileWalk(f, elem, partial && *p=='\0');
  295. if(ff == nil){
  296. vtSetError("%.*s: %R", utfnlen(opath, p-opath), opath);
  297. goto Err;
  298. }
  299. fileDecRef(f);
  300. f = ff;
  301. }
  302. if(*p == '/')
  303. p++;
  304. path = p;
  305. }
  306. return f;
  307. Err:
  308. fileDecRef(f);
  309. return nil;
  310. }
  311. File*
  312. fileOpen(Fs *fs, char *path)
  313. {
  314. return _fileOpen(fs, path, 0);
  315. }
  316. static void
  317. fileSetTmp(File *f, int istmp)
  318. {
  319. int i;
  320. Entry e;
  321. Source *r;
  322. for(i=0; i<2; i++){
  323. if(i==0)
  324. r = f->source;
  325. else
  326. r = f->msource;
  327. if(r == nil)
  328. continue;
  329. if(!sourceGetEntry(r, &e)){
  330. fprint(2, "sourceGetEntry failed (cannot happen): %r\n");
  331. continue;
  332. }
  333. if(istmp)
  334. e.flags |= VtEntryNoArchive;
  335. else
  336. e.flags &= ~VtEntryNoArchive;
  337. if(!sourceSetEntry(r, &e)){
  338. fprint(2, "sourceSetEntry failed (cannot happen): %r\n");
  339. continue;
  340. }
  341. }
  342. }
  343. File *
  344. fileCreate(File *f, char *elem, ulong mode, char *uid)
  345. {
  346. File *ff;
  347. DirEntry *dir;
  348. Source *pr, *r, *mr;
  349. int isdir;
  350. if(!fileLock(f))
  351. return nil;
  352. r = nil;
  353. mr = nil;
  354. for(ff = f->down; ff; ff=ff->next){
  355. if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
  356. ff = nil;
  357. vtSetError(EExists);
  358. goto Err1;
  359. }
  360. }
  361. ff = dirLookup(f, elem);
  362. if(ff != nil){
  363. vtSetError(EExists);
  364. goto Err1;
  365. }
  366. pr = f->source;
  367. if(pr->mode != OReadWrite){
  368. vtSetError(EReadOnly);
  369. goto Err1;
  370. }
  371. if(!sourceLock2(f->source, f->msource, -1))
  372. goto Err1;
  373. ff = fileAlloc(f->fs);
  374. isdir = mode & ModeDir;
  375. r = sourceCreate(pr, pr->dsize, isdir, 0);
  376. if(r == nil)
  377. goto Err;
  378. if(isdir){
  379. mr = sourceCreate(pr, pr->dsize, 0, r->offset);
  380. if(mr == nil)
  381. goto Err;
  382. }
  383. dir = &ff->dir;
  384. dir->elem = vtStrDup(elem);
  385. dir->entry = r->offset;
  386. dir->gen = r->gen;
  387. if(isdir){
  388. dir->mentry = mr->offset;
  389. dir->mgen = mr->gen;
  390. }
  391. dir->size = 0;
  392. if(!fsNextQid(f->fs, &dir->qid))
  393. goto Err;
  394. dir->uid = vtStrDup(uid);
  395. dir->gid = vtStrDup(f->dir.gid);
  396. dir->mid = vtStrDup(uid);
  397. dir->mtime = time(0L);
  398. dir->mcount = 0;
  399. dir->ctime = dir->mtime;
  400. dir->atime = dir->mtime;
  401. dir->mode = mode;
  402. ff->boff = fileMetaAlloc(f, dir, 0);
  403. if(ff->boff == NilBlock)
  404. goto Err;
  405. sourceUnlock(f->source);
  406. sourceUnlock(f->msource);
  407. ff->source = r;
  408. ff->msource = mr;
  409. if(mode&ModeTemporary){
  410. if(!sourceLock2(r, mr, -1))
  411. goto Err1;
  412. fileSetTmp(ff, 1);
  413. sourceUnlock(r);
  414. if(mr)
  415. sourceUnlock(mr);
  416. }
  417. /* committed */
  418. /* link in and up parent ref count */
  419. ff->next = f->down;
  420. f->down = ff;
  421. ff->up = f;
  422. fileIncRef(f);
  423. fileWAccess(f, uid);
  424. fileUnlock(f);
  425. return ff;
  426. Err:
  427. sourceUnlock(f->source);
  428. sourceUnlock(f->msource);
  429. Err1:
  430. if(r){
  431. sourceLock(r, -1);
  432. sourceRemove(r);
  433. }
  434. if(mr){
  435. sourceLock(mr, -1);
  436. sourceRemove(mr);
  437. }
  438. if(ff)
  439. fileDecRef(ff);
  440. fileUnlock(f);
  441. return 0;
  442. }
  443. int
  444. fileRead(File *f, void *buf, int cnt, vlong offset)
  445. {
  446. Source *s;
  447. uvlong size;
  448. u32int bn;
  449. int off, dsize, n, nn;
  450. Block *b;
  451. uchar *p;
  452. if(0)fprint(2, "fileRead: %s %d, %lld\n", f->dir.elem, cnt, offset);
  453. if(!fileRLock(f))
  454. return -1;
  455. if(offset < 0){
  456. vtSetError(EBadOffset);
  457. goto Err1;
  458. }
  459. fileRAccess(f);
  460. if(!sourceLock(f->source, OReadOnly))
  461. goto Err1;
  462. s = f->source;
  463. dsize = s->dsize;
  464. size = sourceGetSize(s);
  465. if(offset >= size)
  466. offset = size;
  467. if(cnt > size-offset)
  468. cnt = size-offset;
  469. bn = offset/dsize;
  470. off = offset%dsize;
  471. p = buf;
  472. while(cnt > 0){
  473. b = sourceBlock(s, bn, OReadOnly);
  474. if(b == nil)
  475. goto Err;
  476. n = cnt;
  477. if(n > dsize-off)
  478. n = dsize-off;
  479. nn = dsize-off;
  480. if(nn > n)
  481. nn = n;
  482. memmove(p, b->data+off, nn);
  483. memset(p+nn, 0, nn-n);
  484. off = 0;
  485. bn++;
  486. cnt -= n;
  487. p += n;
  488. blockPut(b);
  489. }
  490. sourceUnlock(s);
  491. fileRUnlock(f);
  492. return p-(uchar*)buf;
  493. Err:
  494. sourceUnlock(s);
  495. Err1:
  496. fileRUnlock(f);
  497. return -1;
  498. }
  499. /*
  500. * Changes the file block bn to be the given block score.
  501. * Very sneaky. Only used by flfmt.
  502. */
  503. int
  504. fileMapBlock(File *f, ulong bn, uchar score[VtScoreSize], ulong tag)
  505. {
  506. Block *b;
  507. Entry e;
  508. Source *s;
  509. if(!fileLock(f))
  510. return 0;
  511. s = nil;
  512. if(f->dir.mode & ModeDir){
  513. vtSetError(ENotFile);
  514. goto Err;
  515. }
  516. if(f->source->mode != OReadWrite){
  517. vtSetError(EReadOnly);
  518. goto Err;
  519. }
  520. if(!sourceLock(f->source, -1))
  521. goto Err;
  522. s = f->source;
  523. b = _sourceBlock(s, bn, OReadWrite, 1, tag);
  524. if(b == nil)
  525. goto Err;
  526. if(!sourceGetEntry(s, &e))
  527. goto Err;
  528. if(b->l.type == BtDir){
  529. memmove(e.score, score, VtScoreSize);
  530. assert(e.tag == tag || e.tag == 0);
  531. e.tag = tag;
  532. e.flags |= VtEntryLocal;
  533. entryPack(&e, b->data, f->source->offset % f->source->epb);
  534. }else
  535. memmove(b->data + (bn%(e.psize/VtScoreSize))*VtScoreSize, score, VtScoreSize);
  536. blockDirty(b);
  537. blockPut(b);
  538. sourceUnlock(s);
  539. fileUnlock(f);
  540. return 1;
  541. Err:
  542. if(s)
  543. sourceUnlock(s);
  544. fileUnlock(f);
  545. return 0;
  546. }
  547. int
  548. fileSetSize(File *f, uvlong size)
  549. {
  550. int r;
  551. if(!fileLock(f))
  552. return 0;
  553. r = 0;
  554. if(f->dir.mode & ModeDir){
  555. vtSetError(ENotFile);
  556. goto Err;
  557. }
  558. if(f->source->mode != OReadWrite){
  559. vtSetError(EReadOnly);
  560. goto Err;
  561. }
  562. if(!sourceLock(f->source, -1))
  563. goto Err;
  564. r = sourceSetSize(f->source, size);
  565. sourceUnlock(f->source);
  566. Err:
  567. fileUnlock(f);
  568. return r;
  569. }
  570. int
  571. fileWrite(File *f, void *buf, int cnt, vlong offset, char *uid)
  572. {
  573. Source *s;
  574. ulong bn;
  575. int off, dsize, n;
  576. Block *b;
  577. uchar *p;
  578. vlong eof;
  579. if(0)fprint(2, "fileWrite: %s %d, %lld\n", f->dir.elem, cnt, offset);
  580. if(!fileLock(f))
  581. return -1;
  582. s = nil;
  583. if(f->dir.mode & ModeDir){
  584. vtSetError(ENotFile);
  585. goto Err;
  586. }
  587. if(f->source->mode != OReadWrite){
  588. vtSetError(EReadOnly);
  589. goto Err;
  590. }
  591. if(offset < 0){
  592. vtSetError(EBadOffset);
  593. goto Err;
  594. }
  595. fileWAccess(f, uid);
  596. if(!sourceLock(f->source, -1))
  597. goto Err;
  598. s = f->source;
  599. dsize = s->dsize;
  600. eof = sourceGetSize(s);
  601. if(f->dir.mode & ModeAppend)
  602. offset = eof;
  603. bn = offset/dsize;
  604. off = offset%dsize;
  605. p = buf;
  606. while(cnt > 0){
  607. n = cnt;
  608. if(n > dsize-off)
  609. n = dsize-off;
  610. b = sourceBlock(s, bn, n<dsize?OReadWrite:OOverWrite);
  611. if(b == nil){
  612. if(offset > eof)
  613. sourceSetSize(s, offset);
  614. goto Err;
  615. }
  616. memmove(b->data+off, p, n);
  617. off = 0;
  618. cnt -= n;
  619. p += n;
  620. offset += n;
  621. bn++;
  622. blockDirty(b);
  623. blockPut(b);
  624. }
  625. if(offset > eof && !sourceSetSize(s, offset))
  626. goto Err;
  627. sourceUnlock(s);
  628. fileUnlock(f);
  629. return p-(uchar*)buf;
  630. Err:
  631. if(s)
  632. sourceUnlock(s);
  633. fileUnlock(f);
  634. return -1;
  635. }
  636. int
  637. fileGetDir(File *f, DirEntry *dir)
  638. {
  639. if(!fileRLock(f))
  640. return 0;
  641. fileMetaLock(f);
  642. deCopy(dir, &f->dir);
  643. fileMetaUnlock(f);
  644. if(!fileIsDir(f)){
  645. if(!sourceLock(f->source, OReadOnly)){
  646. fileRUnlock(f);
  647. return 0;
  648. }
  649. dir->size = sourceGetSize(f->source);
  650. sourceUnlock(f->source);
  651. }
  652. fileRUnlock(f);
  653. return 1;
  654. }
  655. int
  656. fileTruncate(File *f, char *uid)
  657. {
  658. if(fileIsDir(f)){
  659. vtSetError(ENotFile);
  660. return 0;
  661. }
  662. if(!fileLock(f))
  663. return 0;
  664. if(f->source->mode != OReadWrite){
  665. vtSetError(EReadOnly);
  666. fileUnlock(f);
  667. return 0;
  668. }
  669. if(!sourceLock(f->source, -1)){
  670. fileUnlock(f);
  671. return 0;
  672. }
  673. if(!sourceTruncate(f->source)){
  674. sourceUnlock(f->source);
  675. fileUnlock(f);
  676. return 0;
  677. }
  678. sourceUnlock(f->source);
  679. fileUnlock(f);
  680. fileWAccess(f, uid);
  681. return 1;
  682. }
  683. int
  684. fileSetDir(File *f, DirEntry *dir, char *uid)
  685. {
  686. File *ff;
  687. char *oelem;
  688. u32int mask;
  689. u64int size;
  690. /* can not set permissions for the root */
  691. if(fileIsRoot(f)){
  692. vtSetError(ERoot);
  693. return 0;
  694. }
  695. if(!fileLock(f))
  696. return 0;
  697. if(f->source->mode != OReadWrite){
  698. vtSetError(EReadOnly);
  699. fileUnlock(f);
  700. return 0;
  701. }
  702. fileMetaLock(f);
  703. /* check new name does not already exist */
  704. if(strcmp(f->dir.elem, dir->elem) != 0){
  705. for(ff = f->up->down; ff; ff=ff->next){
  706. if(strcmp(dir->elem, ff->dir.elem) == 0 && !ff->removed){
  707. vtSetError(EExists);
  708. goto Err;
  709. }
  710. }
  711. ff = dirLookup(f->up, dir->elem);
  712. if(ff != nil){
  713. fileDecRef(ff);
  714. vtSetError(EExists);
  715. goto Err;
  716. }
  717. }
  718. if(!sourceLock2(f->source, f->msource, -1))
  719. goto Err;
  720. if(!fileIsDir(f)){
  721. size = sourceGetSize(f->source);
  722. if(size != dir->size){
  723. if(!sourceSetSize(f->source, dir->size)){
  724. sourceUnlock(f->source);
  725. if(f->msource)
  726. sourceUnlock(f->msource);
  727. goto Err;
  728. }
  729. /* commited to changing it now */
  730. }
  731. }
  732. /* commited to changing it now */
  733. if((f->dir.mode&ModeTemporary) != (dir->mode&ModeTemporary))
  734. fileSetTmp(f, dir->mode&ModeTemporary);
  735. sourceUnlock(f->source);
  736. if(f->msource)
  737. sourceUnlock(f->msource);
  738. oelem = nil;
  739. if(strcmp(f->dir.elem, dir->elem) != 0){
  740. oelem = f->dir.elem;
  741. f->dir.elem = vtStrDup(dir->elem);
  742. }
  743. if(strcmp(f->dir.uid, dir->uid) != 0){
  744. vtMemFree(f->dir.uid);
  745. f->dir.uid = vtStrDup(dir->uid);
  746. }
  747. if(strcmp(f->dir.gid, dir->gid) != 0){
  748. vtMemFree(f->dir.gid);
  749. f->dir.gid = vtStrDup(dir->gid);
  750. }
  751. f->dir.mtime = dir->mtime;
  752. f->dir.atime = dir->atime;
  753. //fprint(2, "mode %x %x ", f->dir.mode, dir->mode);
  754. mask = ~(ModeDir|ModeSnapshot);
  755. f->dir.mode &= ~mask;
  756. f->dir.mode |= mask & dir->mode;
  757. f->dirty = 1;
  758. //fprint(2, "->%x\n", f->dir.mode);
  759. fileMetaFlush2(f, oelem);
  760. vtMemFree(oelem);
  761. fileMetaUnlock(f);
  762. fileUnlock(f);
  763. fileWAccess(f->up, uid);
  764. return 1;
  765. Err:
  766. fileMetaUnlock(f);
  767. fileUnlock(f);
  768. return 0;
  769. }
  770. int
  771. fileSetQidSpace(File *f, u64int offset, u64int max)
  772. {
  773. int ret;
  774. if(!fileLock(f))
  775. return 0;
  776. fileMetaLock(f);
  777. f->dir.qidSpace = 1;
  778. f->dir.qidOffset = offset;
  779. f->dir.qidMax = max;
  780. ret = fileMetaFlush2(f, nil)>=0;
  781. fileMetaUnlock(f);
  782. fileUnlock(f);
  783. return ret;
  784. }
  785. uvlong
  786. fileGetId(File *f)
  787. {
  788. /* immutable */
  789. return f->dir.qid;
  790. }
  791. ulong
  792. fileGetMcount(File *f)
  793. {
  794. ulong mcount;
  795. fileMetaLock(f);
  796. mcount = f->dir.mcount;
  797. fileMetaUnlock(f);
  798. return mcount;
  799. }
  800. ulong
  801. fileGetMode(File *f)
  802. {
  803. ulong mode;
  804. fileMetaLock(f);
  805. mode = f->dir.mode;
  806. fileMetaUnlock(f);
  807. return mode;
  808. }
  809. int
  810. fileIsDir(File *f)
  811. {
  812. /* immutable */
  813. return (f->dir.mode & ModeDir) != 0;
  814. }
  815. int
  816. fileIsRoot(File *f)
  817. {
  818. return f == f->fs->file;
  819. }
  820. int
  821. fileIsRoFs(File *f)
  822. {
  823. return f->fs->mode == OReadOnly;
  824. }
  825. int
  826. fileGetSize(File *f, uvlong *size)
  827. {
  828. if(!fileRLock(f))
  829. return 0;
  830. if(!sourceLock(f->source, OReadOnly)){
  831. fileRUnlock(f);
  832. return 0;
  833. }
  834. *size = sourceGetSize(f->source);
  835. sourceUnlock(f->source);
  836. fileRUnlock(f);
  837. return 1;
  838. }
  839. int
  840. fileMetaFlush(File *f, int rec)
  841. {
  842. File **kids, *p;
  843. int nkids;
  844. int i, rv;
  845. fileMetaLock(f);
  846. rv = fileMetaFlush2(f, nil);
  847. fileMetaUnlock(f);
  848. if(!rec || !fileIsDir(f))
  849. return rv;
  850. if(!fileLock(f))
  851. return rv;
  852. nkids = 0;
  853. for(p=f->down; p; p=p->next)
  854. nkids++;
  855. kids = vtMemAlloc(nkids*sizeof(File*));
  856. i = 0;
  857. for(p=f->down; p; p=p->next){
  858. kids[i++] = p;
  859. p->ref++;
  860. }
  861. fileUnlock(f);
  862. for(i=0; i<nkids; i++){
  863. rv |= fileMetaFlush(kids[i], 1);
  864. fileDecRef(kids[i]);
  865. }
  866. vtMemFree(kids);
  867. return rv;
  868. }
  869. /* assumes metaLock is held */
  870. static int
  871. fileMetaFlush2(File *f, char *oelem)
  872. {
  873. File *fp;
  874. Block *b, *bb;
  875. MetaBlock mb;
  876. MetaEntry me, me2;
  877. int i, n;
  878. u32int boff;
  879. if(!f->dirty)
  880. return 0;
  881. if(oelem == nil)
  882. oelem = f->dir.elem;
  883. //print("fileMetaFlush %s->%s\n", oelem, f->dir.elem);
  884. fp = f->up;
  885. if(!sourceLock(fp->msource, -1))
  886. return -1;
  887. /* can happen if source is clri'ed out from under us */
  888. if(f->boff == NilBlock)
  889. goto Err1;
  890. b = sourceBlock(fp->msource, f->boff, OReadWrite);
  891. if(b == nil)
  892. goto Err1;
  893. if(!mbUnpack(&mb, b->data, fp->msource->dsize))
  894. goto Err;
  895. if(!mbSearch(&mb, oelem, &i, &me))
  896. goto Err;
  897. n = deSize(&f->dir);
  898. if(0)fprint(2, "old size %d new size %d\n", me.size, n);
  899. if(mbResize(&mb, &me, n)){
  900. /* fits in the block */
  901. mbDelete(&mb, i);
  902. if(strcmp(f->dir.elem, oelem) != 0)
  903. mbSearch(&mb, f->dir.elem, &i, &me2);
  904. dePack(&f->dir, &me);
  905. mbInsert(&mb, i, &me);
  906. mbPack(&mb);
  907. blockDirty(b);
  908. blockPut(b);
  909. sourceUnlock(fp->msource);
  910. f->dirty = 0;
  911. return 1;
  912. }
  913. /*
  914. * moving entry to another block
  915. * it is feasible for the fs to crash leaving two copies
  916. * of the directory entry. This is just too much work to
  917. * fix. Given that entries are only allocated in a block that
  918. * is less than PercentageFull, most modifications of meta data
  919. * will fit within the block. i.e. this code should almost
  920. * never be executed.
  921. */
  922. boff = fileMetaAlloc(fp, &f->dir, f->boff+1);
  923. if(boff == NilBlock){
  924. /* mbResize might have modified block */
  925. mbPack(&mb);
  926. blockDirty(b);
  927. goto Err;
  928. }
  929. fprint(2, "fileMetaFlush moving entry from %ud -> %ud\n", f->boff, boff);
  930. f->boff = boff;
  931. /* make sure deletion goes to disk after new entry */
  932. bb = sourceBlock(fp->msource, f->boff, OReadWrite);
  933. mbDelete(&mb, i);
  934. mbPack(&mb);
  935. blockDependency(b, bb, -1, nil, nil);
  936. blockPut(bb);
  937. blockDirty(b);
  938. blockPut(b);
  939. sourceUnlock(fp->msource);
  940. f->dirty = 0;
  941. return 1;
  942. Err:
  943. blockPut(b);
  944. Err1:
  945. sourceUnlock(fp->msource);
  946. return -1;
  947. }
  948. static int
  949. fileMetaRemove(File *f, char *uid)
  950. {
  951. Block *b;
  952. MetaBlock mb;
  953. MetaEntry me;
  954. int i;
  955. File *up;
  956. up = f->up;
  957. fileWAccess(up, uid);
  958. fileMetaLock(f);
  959. sourceLock(up->msource, OReadWrite);
  960. b = sourceBlock(up->msource, f->boff, OReadWrite);
  961. if(b == nil)
  962. goto Err;
  963. if(!mbUnpack(&mb, b->data, up->msource->dsize))
  964. {
  965. fprint(2, "U\n");
  966. goto Err;
  967. }
  968. if(!mbSearch(&mb, f->dir.elem, &i, &me))
  969. {
  970. fprint(2, "S\n");
  971. goto Err;
  972. }
  973. mbDelete(&mb, i);
  974. mbPack(&mb);
  975. sourceUnlock(up->msource);
  976. blockDirty(b);
  977. blockPut(b);
  978. f->removed = 1;
  979. f->boff = NilBlock;
  980. f->dirty = 0;
  981. fileMetaUnlock(f);
  982. return 1;
  983. Err:
  984. sourceUnlock(up->msource);
  985. blockPut(b);
  986. fileMetaUnlock(f);
  987. return 0;
  988. }
  989. /* assume file is locked, assume f->msource is locked */
  990. static int
  991. fileCheckEmpty(File *f)
  992. {
  993. u32int i, n;
  994. Block *b;
  995. MetaBlock mb;
  996. Source *r;
  997. r = f->msource;
  998. n = (sourceGetSize(r)+r->dsize-1)/r->dsize;
  999. for(i=0; i<n; i++){
  1000. b = sourceBlock(r, i, OReadOnly);
  1001. if(b == nil)
  1002. goto Err;
  1003. if(!mbUnpack(&mb, b->data, r->dsize))
  1004. goto Err;
  1005. if(mb.nindex > 0){
  1006. vtSetError(ENotEmpty);
  1007. goto Err;
  1008. }
  1009. blockPut(b);
  1010. }
  1011. return 1;
  1012. Err:
  1013. blockPut(b);
  1014. return 0;
  1015. }
  1016. int
  1017. fileRemove(File *f, char *uid)
  1018. {
  1019. File *ff;
  1020. /* can not remove the root */
  1021. if(fileIsRoot(f)){
  1022. vtSetError(ERoot);
  1023. return 0;
  1024. }
  1025. if(!fileLock(f))
  1026. return 0;
  1027. if(f->source->mode != OReadWrite){
  1028. vtSetError(EReadOnly);
  1029. goto Err1;
  1030. }
  1031. if(!sourceLock2(f->source, f->msource, -1))
  1032. goto Err1;
  1033. if(fileIsDir(f) && !fileCheckEmpty(f))
  1034. goto Err;
  1035. for(ff=f->down; ff; ff=ff->next)
  1036. assert(ff->removed);
  1037. sourceRemove(f->source);
  1038. f->source = nil;
  1039. if(f->msource){
  1040. sourceRemove(f->msource);
  1041. f->msource = nil;
  1042. }
  1043. fileUnlock(f);
  1044. if(!fileMetaRemove(f, uid))
  1045. return 0;
  1046. return 1;
  1047. Err:
  1048. sourceUnlock(f->source);
  1049. if(f->msource)
  1050. sourceUnlock(f->msource);
  1051. Err1:
  1052. fileUnlock(f);
  1053. return 0;
  1054. }
  1055. static int
  1056. clri(File *f, char *uid)
  1057. {
  1058. int r;
  1059. if(f == nil)
  1060. return 0;
  1061. if(f->up->source->mode != OReadWrite){
  1062. vtSetError(EReadOnly);
  1063. fileDecRef(f);
  1064. return 0;
  1065. }
  1066. r = fileMetaRemove(f, uid);
  1067. fileDecRef(f);
  1068. return r;
  1069. }
  1070. int
  1071. fileClriPath(Fs *fs, char *path, char *uid)
  1072. {
  1073. return clri(_fileOpen(fs, path, 1), uid);
  1074. }
  1075. int
  1076. fileClri(File *dir, char *elem, char *uid)
  1077. {
  1078. return clri(_fileWalk(dir, elem, 1), uid);
  1079. }
  1080. File *
  1081. fileIncRef(File *vf)
  1082. {
  1083. fileMetaLock(vf);
  1084. assert(vf->ref > 0);
  1085. vf->ref++;
  1086. fileMetaUnlock(vf);
  1087. return vf;
  1088. }
  1089. int
  1090. fileDecRef(File *f)
  1091. {
  1092. File *p, *q, **qq;
  1093. if(f->up == nil){
  1094. /* never linked in */
  1095. assert(f->ref == 1);
  1096. fileFree(f);
  1097. return 1;
  1098. }
  1099. fileMetaLock(f);
  1100. f->ref--;
  1101. if(f->ref > 0){
  1102. fileMetaUnlock(f);
  1103. return 0;
  1104. }
  1105. assert(f->ref == 0);
  1106. assert(f->down == nil);
  1107. fileMetaFlush2(f, nil);
  1108. p = f->up;
  1109. qq = &p->down;
  1110. for(q = *qq; q; q = *qq){
  1111. if(q == f)
  1112. break;
  1113. qq = &q->next;
  1114. }
  1115. assert(q != nil);
  1116. *qq = f->next;
  1117. fileMetaUnlock(f);
  1118. fileFree(f);
  1119. fileDecRef(p);
  1120. return 1;
  1121. }
  1122. File *
  1123. fileGetParent(File *f)
  1124. {
  1125. if(fileIsRoot(f))
  1126. return fileIncRef(f);
  1127. return fileIncRef(f->up);
  1128. }
  1129. DirEntryEnum *
  1130. deeOpen(File *f)
  1131. {
  1132. DirEntryEnum *dee;
  1133. File *p;
  1134. if(!fileIsDir(f)){
  1135. vtSetError(ENotDir);
  1136. fileDecRef(f);
  1137. return nil;
  1138. }
  1139. /* flush out meta data */
  1140. if(!fileLock(f))
  1141. return nil;
  1142. for(p=f->down; p; p=p->next)
  1143. fileMetaFlush2(p, nil);
  1144. fileUnlock(f);
  1145. dee = vtMemAllocZ(sizeof(DirEntryEnum));
  1146. dee->file = fileIncRef(f);
  1147. return dee;
  1148. }
  1149. static int
  1150. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  1151. {
  1152. Block *b;
  1153. ulong bn;
  1154. Entry e;
  1155. int epb;
  1156. epb = s->dsize/VtEntrySize;
  1157. bn = elem/epb;
  1158. elem -= bn*epb;
  1159. b = sourceBlock(s, bn, OReadOnly);
  1160. if(b == nil)
  1161. goto Err;
  1162. if(!entryUnpack(&e, b->data, elem))
  1163. goto Err;
  1164. /* hanging entries are returned as zero size */
  1165. if(!(e.flags & VtEntryActive) || e.gen != gen)
  1166. *size = 0;
  1167. else
  1168. *size = e.size;
  1169. blockPut(b);
  1170. return 1;
  1171. Err:
  1172. blockPut(b);
  1173. return 0;
  1174. }
  1175. static int
  1176. deeFill(DirEntryEnum *dee)
  1177. {
  1178. int i, n;
  1179. Source *meta, *source;
  1180. MetaBlock mb;
  1181. MetaEntry me;
  1182. File *f;
  1183. Block *b;
  1184. DirEntry *de;
  1185. /* clean up first */
  1186. for(i=dee->i; i<dee->n; i++)
  1187. deCleanup(dee->buf+i);
  1188. vtMemFree(dee->buf);
  1189. dee->buf = nil;
  1190. dee->i = 0;
  1191. dee->n = 0;
  1192. f = dee->file;
  1193. source = f->source;
  1194. meta = f->msource;
  1195. b = sourceBlock(meta, dee->boff, OReadOnly);
  1196. if(b == nil)
  1197. goto Err;
  1198. if(!mbUnpack(&mb, b->data, meta->dsize))
  1199. goto Err;
  1200. n = mb.nindex;
  1201. dee->buf = vtMemAlloc(n * sizeof(DirEntry));
  1202. for(i=0; i<n; i++){
  1203. de = dee->buf + i;
  1204. meUnpack(&me, &mb, i);
  1205. if(!deUnpack(de, &me))
  1206. goto Err;
  1207. dee->n++;
  1208. if(!(de->mode & ModeDir))
  1209. if(!dirEntrySize(source, de->entry, de->gen, &de->size))
  1210. goto Err;
  1211. }
  1212. dee->boff++;
  1213. blockPut(b);
  1214. return 1;
  1215. Err:
  1216. blockPut(b);
  1217. return 0;
  1218. }
  1219. int
  1220. deeRead(DirEntryEnum *dee, DirEntry *de)
  1221. {
  1222. int ret, didread;
  1223. File *f;
  1224. u32int nb;
  1225. if(dee == nil){
  1226. vtSetError("cannot happen in deeRead");
  1227. return -1;
  1228. }
  1229. f = dee->file;
  1230. if(!fileRLock(f))
  1231. return -1;
  1232. if(!sourceLock2(f->source, f->msource, OReadOnly)){
  1233. fileRUnlock(f);
  1234. return -1;
  1235. }
  1236. nb = (sourceGetSize(f->msource)+f->msource->dsize-1)/f->msource->dsize;
  1237. didread = 0;
  1238. while(dee->i >= dee->n){
  1239. if(dee->boff >= nb){
  1240. ret = 0;
  1241. goto Return;
  1242. }
  1243. didread = 1;
  1244. if(!deeFill(dee)){
  1245. ret = -1;
  1246. goto Return;
  1247. }
  1248. }
  1249. memmove(de, dee->buf + dee->i, sizeof(DirEntry));
  1250. dee->i++;
  1251. ret = 1;
  1252. Return:
  1253. sourceUnlock(f->source);
  1254. sourceUnlock(f->msource);
  1255. fileRUnlock(f);
  1256. if(didread)
  1257. fileRAccess(f);
  1258. return ret;
  1259. }
  1260. void
  1261. deeClose(DirEntryEnum *dee)
  1262. {
  1263. int i;
  1264. if(dee == nil)
  1265. return;
  1266. for(i=dee->i; i<dee->n; i++)
  1267. deCleanup(dee->buf+i);
  1268. vtMemFree(dee->buf);
  1269. fileDecRef(dee->file);
  1270. vtMemFree(dee);
  1271. }
  1272. /*
  1273. * caller must lock f->source and f->msource
  1274. * caller must NOT lock the source and msource
  1275. * referenced by dir.
  1276. */
  1277. static u32int
  1278. fileMetaAlloc(File *f, DirEntry *dir, u32int start)
  1279. {
  1280. u32int nb, bo;
  1281. Block *b, *bb;
  1282. MetaBlock mb;
  1283. int nn;
  1284. uchar *p;
  1285. int i, n, epb;
  1286. MetaEntry me;
  1287. Source *s, *ms;
  1288. s = f->source;
  1289. ms = f->msource;
  1290. n = deSize(dir);
  1291. nb = (sourceGetSize(ms)+ms->dsize-1)/ms->dsize;
  1292. b = nil;
  1293. if(start > nb)
  1294. start = nb;
  1295. for(bo=start; bo<nb; bo++){
  1296. b = sourceBlock(ms, bo, OReadWrite);
  1297. if(b == nil)
  1298. goto Err;
  1299. if(!mbUnpack(&mb, b->data, ms->dsize))
  1300. goto Err;
  1301. nn = (mb.maxsize*FullPercentage/100) - mb.size + mb.free;
  1302. if(n <= nn && mb.nindex < mb.maxindex)
  1303. break;
  1304. blockPut(b);
  1305. b = nil;
  1306. }
  1307. /* add block to meta file */
  1308. if(b == nil){
  1309. b = sourceBlock(ms, bo, OReadWrite);
  1310. if(b == nil)
  1311. goto Err;
  1312. sourceSetSize(ms, (nb+1)*ms->dsize);
  1313. mbInit(&mb, b->data, ms->dsize, ms->dsize/BytesPerEntry);
  1314. }
  1315. p = mbAlloc(&mb, n);
  1316. if(p == nil){
  1317. /* mbAlloc might have changed block */
  1318. mbPack(&mb);
  1319. blockDirty(b);
  1320. vtSetError(EBadMeta);
  1321. goto Err;
  1322. }
  1323. mbSearch(&mb, dir->elem, &i, &me);
  1324. assert(me.p == nil);
  1325. me.p = p;
  1326. me.size = n;
  1327. dePack(dir, &me);
  1328. mbInsert(&mb, i, &me);
  1329. mbPack(&mb);
  1330. /* meta block depends on super block for qid ... */
  1331. bb = cacheLocal(b->c, PartSuper, 0, OReadOnly);
  1332. blockDependency(b, bb, -1, nil, nil);
  1333. blockPut(bb);
  1334. /* ... and one or two dir entries */
  1335. epb = s->dsize/VtEntrySize;
  1336. bb = sourceBlock(s, dir->entry/epb, OReadOnly);
  1337. blockDependency(b, bb, -1, nil, nil);
  1338. blockPut(bb);
  1339. if(dir->mode & ModeDir){
  1340. bb = sourceBlock(s, dir->mentry/epb, OReadOnly);
  1341. blockDependency(b, bb, -1, nil, nil);
  1342. blockPut(bb);
  1343. }
  1344. blockDirty(b);
  1345. blockPut(b);
  1346. return bo;
  1347. Err:
  1348. blockPut(b);
  1349. return NilBlock;
  1350. }
  1351. static int
  1352. chkSource(File *f)
  1353. {
  1354. if(f->partial)
  1355. return 1;
  1356. if(f->source == nil || (f->dir.mode & ModeDir) && f->msource == nil){
  1357. vtSetError(ERemoved);
  1358. return 0;
  1359. }
  1360. return 1;
  1361. }
  1362. static int
  1363. fileRLock(File *f)
  1364. {
  1365. assert(!vtCanLock(f->fs->elk));
  1366. vtRLock(f->lk);
  1367. if(!chkSource(f)){
  1368. fileRUnlock(f);
  1369. return 0;
  1370. }
  1371. return 1;
  1372. }
  1373. static void
  1374. fileRUnlock(File *f)
  1375. {
  1376. vtRUnlock(f->lk);
  1377. }
  1378. static int
  1379. fileLock(File *f)
  1380. {
  1381. assert(!vtCanLock(f->fs->elk));
  1382. vtLock(f->lk);
  1383. if(!chkSource(f)){
  1384. fileUnlock(f);
  1385. return 0;
  1386. }
  1387. return 1;
  1388. }
  1389. static void
  1390. fileUnlock(File *f)
  1391. {
  1392. vtUnlock(f->lk);
  1393. }
  1394. /*
  1395. * f->source and f->msource must NOT be locked.
  1396. * fileMetaFlush locks the fileMeta and then the source (in fileMetaFlush2).
  1397. * We have to respect that ordering.
  1398. */
  1399. static void
  1400. fileMetaLock(File *f)
  1401. {
  1402. if(f->up == nil)
  1403. fprint(2, "f->elem = %s\n", f->dir.elem);
  1404. assert(f->up != nil);
  1405. assert(!vtCanLock(f->fs->elk));
  1406. vtLock(f->up->lk);
  1407. }
  1408. static void
  1409. fileMetaUnlock(File *f)
  1410. {
  1411. vtUnlock(f->up->lk);
  1412. }
  1413. /*
  1414. * f->source and f->msource must NOT be locked.
  1415. * see fileMetaLock.
  1416. */
  1417. static void
  1418. fileRAccess(File* f)
  1419. {
  1420. if(f->mode == OReadOnly)
  1421. return;
  1422. fileMetaLock(f);
  1423. f->dir.atime = time(0L);
  1424. f->dirty = 1;
  1425. fileMetaUnlock(f);
  1426. }
  1427. /*
  1428. * f->source and f->msource must NOT be locked.
  1429. * see fileMetaLock.
  1430. */
  1431. static void
  1432. fileWAccess(File* f, char *mid)
  1433. {
  1434. if(f->mode == OReadOnly)
  1435. return;
  1436. fileMetaLock(f);
  1437. f->dir.atime = f->dir.mtime = time(0L);
  1438. if(strcmp(f->dir.mid, mid) != 0){
  1439. vtMemFree(f->dir.mid);
  1440. f->dir.mid = vtStrDup(mid);
  1441. }
  1442. f->dir.mcount++;
  1443. f->dirty = 1;
  1444. fileMetaUnlock(f);
  1445. /*RSC: let's try this */
  1446. /*presotto - lets not
  1447. if(f->up)
  1448. fileWAccess(f->up, mid);
  1449. */
  1450. }
  1451. static int
  1452. getEntry(Source *r, Entry *e, int checkepoch)
  1453. {
  1454. u32int epoch;
  1455. Block *b;
  1456. if(r == nil){
  1457. memset(&e, 0, sizeof e);
  1458. return 1;
  1459. }
  1460. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadOnly);
  1461. if(b == nil)
  1462. return 0;
  1463. if(!entryUnpack(e, b->data, r->offset % r->epb)){
  1464. blockPut(b);
  1465. return 0;
  1466. }
  1467. epoch = b->l.epoch;
  1468. blockPut(b);
  1469. if(checkepoch){
  1470. b = cacheGlobal(r->fs->cache, e->score, entryType(e), e->tag, OReadOnly);
  1471. if(b){
  1472. if(b->l.epoch >= epoch)
  1473. fprint(2, "warning: entry %p epoch not older %#.8ux/%d %V/%d in getEntry\n",
  1474. r, b->addr, b->l.epoch, r->score, epoch);
  1475. blockPut(b);
  1476. }
  1477. }
  1478. return 1;
  1479. }
  1480. static int
  1481. setEntry(Source *r, Entry *e)
  1482. {
  1483. Block *b;
  1484. Entry oe;
  1485. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadWrite);
  1486. if(0) fprint(2, "setEntry: b %#ux %d score=%V\n", b->addr, r->offset % r->epb, e->score);
  1487. if(b == nil)
  1488. return 0;
  1489. if(!entryUnpack(&oe, b->data, r->offset % r->epb)){
  1490. blockPut(b);
  1491. return 0;
  1492. }
  1493. e->gen = oe.gen;
  1494. entryPack(e, b->data, r->offset % r->epb);
  1495. /* BUG b should depend on the entry pointer */
  1496. blockDirty(b);
  1497. blockPut(b);
  1498. return 1;
  1499. }
  1500. /* assumes hold elk */
  1501. int
  1502. fileSnapshot(File *dst, File *src, u32int epoch, int doarchive)
  1503. {
  1504. Entry e, ee;
  1505. /* add link to snapshot */
  1506. if(!getEntry(src->source, &e, 1) || !getEntry(src->msource, &ee, 1))
  1507. return 0;
  1508. e.snap = epoch;
  1509. e.archive = doarchive;
  1510. ee.snap = epoch;
  1511. ee.archive = doarchive;
  1512. if(!setEntry(dst->source, &e) || !setEntry(dst->msource, &ee))
  1513. return 0;
  1514. return 1;
  1515. }
  1516. int
  1517. fileGetSources(File *f, Entry *e, Entry *ee)
  1518. {
  1519. if(!getEntry(f->source, e, 0)
  1520. || !getEntry(f->msource, ee, 0))
  1521. return 0;
  1522. return 1;
  1523. }
  1524. /*
  1525. * Walk down to the block(s) containing the Entries
  1526. * for f->source and f->msource, copying as we go.
  1527. */
  1528. int
  1529. fileWalkSources(File *f)
  1530. {
  1531. if(f->mode == OReadOnly){
  1532. fprint(2, "readonly in fileWalkSources\n");
  1533. return 1;
  1534. }
  1535. if(!sourceLock2(f->source, f->msource, OReadWrite)){
  1536. fprint(2, "sourceLock2 failed in fileWalkSources\n");
  1537. return 0;
  1538. }
  1539. sourceUnlock(f->source);
  1540. sourceUnlock(f->msource);
  1541. return 1;
  1542. }