file.c 29 KB

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