file.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. File *
  313. fileCreate(File *f, char *elem, ulong mode, char *uid)
  314. {
  315. File *ff;
  316. DirEntry *dir;
  317. Source *pr, *r, *mr;
  318. int isdir;
  319. if(!fileLock(f))
  320. return nil;
  321. r = nil;
  322. mr = nil;
  323. for(ff = f->down; ff; ff=ff->next){
  324. if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
  325. ff = nil;
  326. vtSetError(EExists);
  327. goto Err1;
  328. }
  329. }
  330. ff = dirLookup(f, elem);
  331. if(ff != nil){
  332. vtSetError(EExists);
  333. goto Err1;
  334. }
  335. pr = f->source;
  336. if(pr->mode != OReadWrite){
  337. vtSetError(EReadOnly);
  338. goto Err1;
  339. }
  340. if(!sourceLock2(f->source, f->msource, -1))
  341. goto Err1;
  342. ff = fileAlloc(f->fs);
  343. isdir = mode & ModeDir;
  344. r = sourceCreate(pr, pr->dsize, isdir, 0);
  345. if(r == nil)
  346. goto Err;
  347. if(isdir){
  348. mr = sourceCreate(pr, pr->dsize, 0, r->offset);
  349. if(mr == nil)
  350. goto Err;
  351. }
  352. dir = &ff->dir;
  353. dir->elem = vtStrDup(elem);
  354. dir->entry = r->offset;
  355. dir->gen = r->gen;
  356. if(isdir){
  357. dir->mentry = mr->offset;
  358. dir->mgen = mr->gen;
  359. }
  360. dir->size = 0;
  361. if(!fsNextQid(f->fs, &dir->qid))
  362. goto Err;
  363. dir->uid = vtStrDup(uid);
  364. dir->gid = vtStrDup(f->dir.gid);
  365. dir->mid = vtStrDup(uid);
  366. dir->mtime = time(0L);
  367. dir->mcount = 0;
  368. dir->ctime = dir->mtime;
  369. dir->atime = dir->mtime;
  370. dir->mode = mode;
  371. ff->boff = fileMetaAlloc(f, dir, 0);
  372. if(ff->boff == NilBlock)
  373. goto Err;
  374. /* committed */
  375. sourceUnlock(f->source);
  376. sourceUnlock(f->msource);
  377. ff->source = r;
  378. ff->msource = mr;
  379. /* link in and up parent ref count */
  380. ff->next = f->down;
  381. f->down = ff;
  382. ff->up = f;
  383. fileIncRef(f);
  384. fileWAccess(f, uid);
  385. fileUnlock(f);
  386. return ff;
  387. Err:
  388. sourceUnlock(f->source);
  389. sourceUnlock(f->msource);
  390. Err1:
  391. if(r){
  392. sourceLock(r, -1);
  393. sourceRemove(r);
  394. }
  395. if(mr){
  396. sourceLock(mr, -1);
  397. sourceRemove(mr);
  398. }
  399. if(ff)
  400. fileDecRef(ff);
  401. fileUnlock(f);
  402. return 0;
  403. }
  404. int
  405. fileRead(File *f, void *buf, int cnt, vlong offset)
  406. {
  407. Source *s;
  408. uvlong size;
  409. u32int bn;
  410. int off, dsize, n, nn;
  411. Block *b;
  412. uchar *p;
  413. if(0)fprint(2, "fileRead: %s %d, %lld\n", f->dir.elem, cnt, offset);
  414. if(!fileRLock(f))
  415. return -1;
  416. if(offset < 0){
  417. vtSetError(EBadOffset);
  418. goto Err1;
  419. }
  420. fileRAccess(f);
  421. if(!sourceLock(f->source, OReadOnly))
  422. goto Err1;
  423. s = f->source;
  424. dsize = s->dsize;
  425. size = sourceGetSize(s);
  426. if(offset >= size)
  427. offset = size;
  428. if(cnt > size-offset)
  429. cnt = size-offset;
  430. bn = offset/dsize;
  431. off = offset%dsize;
  432. p = buf;
  433. while(cnt > 0){
  434. b = sourceBlock(s, bn, OReadOnly);
  435. if(b == nil)
  436. goto Err;
  437. n = cnt;
  438. if(n > dsize-off)
  439. n = dsize-off;
  440. nn = dsize-off;
  441. if(nn > n)
  442. nn = n;
  443. memmove(p, b->data+off, nn);
  444. memset(p+nn, 0, nn-n);
  445. off = 0;
  446. bn++;
  447. cnt -= n;
  448. p += n;
  449. blockPut(b);
  450. }
  451. sourceUnlock(s);
  452. fileRUnlock(f);
  453. return p-(uchar*)buf;
  454. Err:
  455. sourceUnlock(s);
  456. Err1:
  457. fileRUnlock(f);
  458. return -1;
  459. }
  460. int
  461. fileWrite(File *f, void *buf, int cnt, vlong offset, char *uid)
  462. {
  463. Source *s;
  464. ulong bn;
  465. int off, dsize, n;
  466. Block *b;
  467. uchar *p;
  468. vlong eof;
  469. if(0)fprint(2, "fileWrite: %s %d, %lld\n", f->dir.elem, cnt, offset);
  470. if(!fileLock(f))
  471. return -1;
  472. s = nil;
  473. if(f->dir.mode & ModeDir){
  474. vtSetError(ENotFile);
  475. goto Err;
  476. }
  477. if(f->source->mode != OReadWrite){
  478. vtSetError(EReadOnly);
  479. goto Err;
  480. }
  481. if(offset < 0){
  482. vtSetError(EBadOffset);
  483. goto Err;
  484. }
  485. fileWAccess(f, uid);
  486. if(!sourceLock(f->source, -1))
  487. goto Err;
  488. s = f->source;
  489. dsize = s->dsize;
  490. eof = sourceGetSize(s);
  491. if(f->dir.mode & ModeAppend)
  492. offset = eof;
  493. bn = offset/dsize;
  494. off = offset%dsize;
  495. p = buf;
  496. while(cnt > 0){
  497. n = cnt;
  498. if(n > dsize-off)
  499. n = dsize-off;
  500. b = sourceBlock(s, bn, n<dsize?OReadWrite:OOverWrite);
  501. if(b == nil){
  502. if(offset > eof)
  503. sourceSetSize(s, offset);
  504. goto Err;
  505. }
  506. memmove(b->data+off, p, n);
  507. off = 0;
  508. cnt -= n;
  509. p += n;
  510. offset += n;
  511. bn++;
  512. blockDirty(b);
  513. blockPut(b);
  514. }
  515. if(offset > eof && !sourceSetSize(s, offset))
  516. goto Err;
  517. sourceUnlock(s);
  518. fileUnlock(f);
  519. return p-(uchar*)buf;
  520. Err:
  521. if(s)
  522. sourceUnlock(s);
  523. fileUnlock(f);
  524. return -1;
  525. }
  526. int
  527. fileGetDir(File *f, DirEntry *dir)
  528. {
  529. if(!fileRLock(f))
  530. return 0;
  531. fileMetaLock(f);
  532. deCopy(dir, &f->dir);
  533. fileMetaUnlock(f);
  534. if(!fileIsDir(f)){
  535. if(!sourceLock(f->source, OReadOnly)){
  536. fileRUnlock(f);
  537. return 0;
  538. }
  539. dir->size = sourceGetSize(f->source);
  540. sourceUnlock(f->source);
  541. }
  542. fileRUnlock(f);
  543. return 1;
  544. }
  545. int
  546. fileTruncate(File *f, char *uid)
  547. {
  548. if(fileIsDir(f)){
  549. vtSetError(ENotFile);
  550. return 0;
  551. }
  552. if(!fileLock(f))
  553. return 0;
  554. if(f->source->mode != OReadWrite){
  555. vtSetError(EReadOnly);
  556. fileUnlock(f);
  557. return 0;
  558. }
  559. if(!sourceLock(f->source, -1)){
  560. fileUnlock(f);
  561. return 0;
  562. }
  563. if(!sourceTruncate(f->source)){
  564. sourceUnlock(f->source);
  565. fileUnlock(f);
  566. return 0;
  567. }
  568. sourceUnlock(f->source);
  569. fileUnlock(f);
  570. fileWAccess(f->up, uid);
  571. return 1;
  572. }
  573. int
  574. fileSetDir(File *f, DirEntry *dir, char *uid)
  575. {
  576. File *ff;
  577. char *oelem;
  578. u32int mask;
  579. u64int size;
  580. /* can not set permissions for the root */
  581. if(fileIsRoot(f)){
  582. vtSetError(ERoot);
  583. return 0;
  584. }
  585. if(!fileLock(f))
  586. return 0;
  587. if(f->source->mode != OReadWrite){
  588. vtSetError(EReadOnly);
  589. fileUnlock(f);
  590. return 0;
  591. }
  592. fileMetaLock(f);
  593. /* check new name does not already exist */
  594. if(strcmp(f->dir.elem, dir->elem) != 0){
  595. for(ff = f->up->down; ff; ff=ff->next){
  596. if(strcmp(dir->elem, ff->dir.elem) == 0 && !ff->removed){
  597. vtSetError(EExists);
  598. goto Err;
  599. }
  600. }
  601. ff = dirLookup(f->up, dir->elem);
  602. if(ff != nil){
  603. fileDecRef(ff);
  604. vtSetError(EExists);
  605. goto Err;
  606. }
  607. }
  608. if(!fileIsDir(f)){
  609. if(!sourceLock(f->source, -1))
  610. goto Err;
  611. size = sourceGetSize(f->source);
  612. if(size != dir->size){
  613. if(!sourceSetSize(f->source, dir->size)){
  614. sourceUnlock(f->source);
  615. goto Err;
  616. }
  617. /* commited to changing it now */
  618. }
  619. sourceUnlock(f->source);
  620. }
  621. /* commited to changing it now */
  622. oelem = nil;
  623. if(strcmp(f->dir.elem, dir->elem) != 0){
  624. oelem = f->dir.elem;
  625. f->dir.elem = vtStrDup(dir->elem);
  626. }
  627. if(strcmp(f->dir.uid, dir->uid) != 0){
  628. vtMemFree(f->dir.uid);
  629. f->dir.uid = vtStrDup(dir->uid);
  630. }
  631. if(strcmp(f->dir.gid, dir->gid) != 0){
  632. vtMemFree(f->dir.gid);
  633. f->dir.gid = vtStrDup(dir->gid);
  634. }
  635. f->dir.mtime = dir->mtime;
  636. f->dir.atime = dir->atime;
  637. //fprint(2, "mode %x %x ", f->dir.mode, dir->mode);
  638. mask = ~(ModeDir|ModeSnapshot);
  639. f->dir.mode &= ~mask;
  640. f->dir.mode |= mask & dir->mode;
  641. f->dirty = 1;
  642. //fprint(2, "->%x\n", f->dir.mode);
  643. fileMetaFlush2(f, oelem);
  644. vtMemFree(oelem);
  645. fileMetaUnlock(f);
  646. fileUnlock(f);
  647. fileWAccess(f->up, uid);
  648. return 1;
  649. Err:
  650. fileMetaUnlock(f);
  651. fileUnlock(f);
  652. return 0;
  653. }
  654. int
  655. fileSetQidSpace(File *f, u64int offset, u64int max)
  656. {
  657. int ret;
  658. if(!fileLock(f))
  659. return 0;
  660. fileMetaLock(f);
  661. f->dir.qidSpace = 1;
  662. f->dir.qidOffset = offset;
  663. f->dir.qidMax = max;
  664. ret = fileMetaFlush2(f, nil);
  665. fileMetaUnlock(f);
  666. fileUnlock(f);
  667. return ret;
  668. }
  669. uvlong
  670. fileGetId(File *f)
  671. {
  672. /* immutable */
  673. return f->dir.qid;
  674. }
  675. ulong
  676. fileGetMcount(File *f)
  677. {
  678. ulong mcount;
  679. fileMetaLock(f);
  680. mcount = f->dir.mcount;
  681. fileMetaUnlock(f);
  682. return mcount;
  683. }
  684. ulong
  685. fileGetMode(File *f)
  686. {
  687. ulong mode;
  688. fileMetaLock(f);
  689. mode = f->dir.mode;
  690. fileMetaUnlock(f);
  691. return mode;
  692. }
  693. int
  694. fileIsDir(File *f)
  695. {
  696. /* immutable */
  697. return (f->dir.mode & ModeDir) != 0;
  698. }
  699. int
  700. fileIsRoot(File *f)
  701. {
  702. return f == f->fs->file;
  703. }
  704. int
  705. fileIsRoFs(File *f)
  706. {
  707. return f->fs->mode == OReadOnly;
  708. }
  709. int
  710. fileGetSize(File *f, uvlong *size)
  711. {
  712. if(!fileRLock(f))
  713. return 0;
  714. if(!sourceLock(f->source, OReadOnly)){
  715. fileRUnlock(f);
  716. return 0;
  717. }
  718. *size = sourceGetSize(f->source);
  719. sourceUnlock(f->source);
  720. fileRUnlock(f);
  721. return 1;
  722. }
  723. void
  724. fileMetaFlush(File *f, int rec)
  725. {
  726. File **kids, *p;
  727. int nkids;
  728. int i;
  729. fileMetaLock(f);
  730. fileMetaFlush2(f, nil);
  731. fileMetaUnlock(f);
  732. if(!rec || !fileIsDir(f))
  733. return;
  734. if(!fileLock(f))
  735. return;
  736. nkids = 0;
  737. for(p=f->down; p; p=p->next)
  738. nkids++;
  739. kids = vtMemAlloc(nkids*sizeof(File*));
  740. i = 0;
  741. for(p=f->down; p; p=p->next){
  742. kids[i++] = p;
  743. p->ref++;
  744. }
  745. fileUnlock(f);
  746. for(i=0; i<nkids; i++){
  747. fileMetaFlush(kids[i], 1);
  748. fileDecRef(kids[i]);
  749. }
  750. vtMemFree(kids);
  751. }
  752. /* assumes metaLock is held */
  753. static int
  754. fileMetaFlush2(File *f, char *oelem)
  755. {
  756. File *fp;
  757. Block *b, *bb;
  758. MetaBlock mb;
  759. MetaEntry me, me2;
  760. int i, n;
  761. u32int boff;
  762. if(!f->dirty)
  763. return 1;
  764. if(oelem == nil)
  765. oelem = f->dir.elem;
  766. //print("fileMetaFlush %s->%s\n", oelem, f->dir.elem);
  767. fp = f->up;
  768. if(!sourceLock(fp->msource, -1))
  769. return 0;
  770. b = sourceBlock(fp->msource, f->boff, OReadWrite);
  771. if(b == nil)
  772. goto Err1;
  773. if(!mbUnpack(&mb, b->data, fp->msource->dsize))
  774. goto Err;
  775. if(!mbSearch(&mb, oelem, &i, &me))
  776. goto Err;
  777. n = deSize(&f->dir);
  778. if(0)fprint(2, "old size %d new size %d\n", me.size, n);
  779. if(mbResize(&mb, &me, n)){
  780. /* fits in the block */
  781. mbDelete(&mb, i);
  782. if(strcmp(f->dir.elem, oelem) != 0)
  783. mbSearch(&mb, f->dir.elem, &i, &me2);
  784. dePack(&f->dir, &me);
  785. mbInsert(&mb, i, &me);
  786. mbPack(&mb);
  787. blockDirty(b);
  788. blockPut(b);
  789. sourceUnlock(fp->msource);
  790. f->dirty = 0;
  791. return 1;
  792. }
  793. /*
  794. * moving entry to another block
  795. * it is feasible for the fs to crash leaving two copies
  796. * of the directory entry. This is just too much work to
  797. * fix. Given that entries are only allocated in a block that
  798. * is less than PercentageFull, most modifications of meta data
  799. * will fit within the block. i.e. this code should almost
  800. * never be executed.
  801. */
  802. boff = fileMetaAlloc(fp, &f->dir, f->boff+1);
  803. if(boff == NilBlock){
  804. /* mbResize might have modified block */
  805. mbPack(&mb);
  806. blockDirty(b);
  807. goto Err;
  808. }
  809. fprint(2, "fileMetaFlush moving entry from %ud -> %ud\n", f->boff, boff);
  810. f->boff = boff;
  811. /* make sure deletion goes to disk after new entry */
  812. bb = sourceBlock(fp->msource, f->boff, OReadWrite);
  813. mbDelete(&mb, i);
  814. mbPack(&mb);
  815. blockDependency(b, bb, -1, nil, nil);
  816. blockPut(bb);
  817. blockDirty(b);
  818. blockPut(b);
  819. sourceUnlock(fp->msource);
  820. f->dirty = 0;
  821. return 1;
  822. Err:
  823. blockPut(b);
  824. Err1:
  825. sourceUnlock(fp->msource);
  826. return 0;
  827. }
  828. static int
  829. fileMetaRemove(File *f, char *uid)
  830. {
  831. Block *b;
  832. MetaBlock mb;
  833. MetaEntry me;
  834. int i;
  835. File *up;
  836. up = f->up;
  837. fileWAccess(up, uid);
  838. fileMetaLock(f);
  839. sourceLock(up->msource, OReadWrite);
  840. b = sourceBlock(up->msource, f->boff, OReadWrite);
  841. if(b == nil)
  842. goto Err;
  843. if(!mbUnpack(&mb, b->data, up->msource->dsize))
  844. {
  845. fprint(2, "U\n");
  846. goto Err;
  847. }
  848. if(!mbSearch(&mb, f->dir.elem, &i, &me))
  849. {
  850. fprint(2, "S\n");
  851. goto Err;
  852. }
  853. mbDelete(&mb, i);
  854. mbPack(&mb);
  855. sourceUnlock(up->msource);
  856. blockDirty(b);
  857. blockPut(b);
  858. f->removed = 1;
  859. f->boff = NilBlock;
  860. f->dirty = 0;
  861. fileMetaUnlock(f);
  862. return 1;
  863. Err:
  864. sourceUnlock(up->msource);
  865. blockPut(b);
  866. fileMetaUnlock(f);
  867. return 0;
  868. }
  869. /* assume file is locked, assume f->msource is locked */
  870. static int
  871. fileCheckEmpty(File *f)
  872. {
  873. u32int i, n;
  874. Block *b;
  875. MetaBlock mb;
  876. Source *r;
  877. r = f->msource;
  878. n = (sourceGetSize(r)+r->dsize-1)/r->dsize;
  879. for(i=0; i<n; i++){
  880. b = sourceBlock(r, i, OReadOnly);
  881. if(b == nil)
  882. goto Err;
  883. if(!mbUnpack(&mb, b->data, r->dsize))
  884. goto Err;
  885. if(mb.nindex > 0){
  886. vtSetError(ENotEmpty);
  887. goto Err;
  888. }
  889. blockPut(b);
  890. }
  891. return 1;
  892. Err:
  893. blockPut(b);
  894. return 0;
  895. }
  896. int
  897. fileRemove(File *f, char *uid)
  898. {
  899. File *ff;
  900. /* can not remove the root */
  901. if(fileIsRoot(f)){
  902. vtSetError(ERoot);
  903. return 0;
  904. }
  905. if(!fileLock(f))
  906. return 0;
  907. if(f->source->mode != OReadWrite){
  908. vtSetError(EReadOnly);
  909. goto Err1;
  910. }
  911. if(!sourceLock2(f->source, f->msource, -1))
  912. goto Err1;
  913. if(fileIsDir(f) && !fileCheckEmpty(f))
  914. goto Err;
  915. for(ff=f->down; ff; ff=ff->next)
  916. assert(ff->removed);
  917. sourceRemove(f->source);
  918. f->source = nil;
  919. if(f->msource){
  920. sourceRemove(f->msource);
  921. f->msource = nil;
  922. }
  923. fileUnlock(f);
  924. if(!fileMetaRemove(f, uid))
  925. return 0;
  926. return 1;
  927. Err:
  928. sourceUnlock(f->source);
  929. if(f->msource)
  930. sourceUnlock(f->msource);
  931. Err1:
  932. fileUnlock(f);
  933. return 0;
  934. }
  935. static int
  936. clri(File *f, char *uid)
  937. {
  938. int r;
  939. if(f == nil)
  940. return 0;
  941. if(f->up->source->mode != OReadWrite){
  942. vtSetError(EReadOnly);
  943. fileDecRef(f);
  944. return 0;
  945. }
  946. r = fileMetaRemove(f, uid);
  947. fileDecRef(f);
  948. return r;
  949. }
  950. int
  951. fileClriPath(Fs *fs, char *path, char *uid)
  952. {
  953. return clri(_fileOpen(fs, path, 1), uid);
  954. }
  955. int
  956. fileClri(File *dir, char *elem, char *uid)
  957. {
  958. return clri(_fileWalk(dir, elem, 1), uid);
  959. }
  960. File *
  961. fileIncRef(File *vf)
  962. {
  963. fileMetaLock(vf);
  964. assert(vf->ref > 0);
  965. vf->ref++;
  966. fileMetaUnlock(vf);
  967. return vf;
  968. }
  969. int
  970. fileDecRef(File *f)
  971. {
  972. File *p, *q, **qq;
  973. if(f->up == nil){
  974. /* never linked in */
  975. assert(f->ref == 1);
  976. fileFree(f);
  977. return 1;
  978. }
  979. fileMetaLock(f);
  980. f->ref--;
  981. if(f->ref > 0){
  982. fileMetaUnlock(f);
  983. return 0;
  984. }
  985. assert(f->ref == 0);
  986. assert(f->down == nil);
  987. fileMetaFlush2(f, nil);
  988. p = f->up;
  989. qq = &p->down;
  990. for(q = *qq; q; q = *qq){
  991. if(q == f)
  992. break;
  993. qq = &q->next;
  994. }
  995. assert(q != nil);
  996. *qq = f->next;
  997. fileMetaUnlock(f);
  998. fileFree(f);
  999. fileDecRef(p);
  1000. return 1;
  1001. }
  1002. File *
  1003. fileGetParent(File *f)
  1004. {
  1005. if(fileIsRoot(f))
  1006. return fileIncRef(f);
  1007. return fileIncRef(f->up);
  1008. }
  1009. DirEntryEnum *
  1010. deeOpen(File *f)
  1011. {
  1012. DirEntryEnum *dee;
  1013. File *p;
  1014. if(!fileIsDir(f)){
  1015. vtSetError(ENotDir);
  1016. fileDecRef(f);
  1017. return nil;
  1018. }
  1019. /* flush out meta data */
  1020. if(!fileLock(f))
  1021. return nil;
  1022. for(p=f->down; p; p=p->next)
  1023. fileMetaFlush2(p, nil);
  1024. fileUnlock(f);
  1025. dee = vtMemAllocZ(sizeof(DirEntryEnum));
  1026. dee->file = fileIncRef(f);
  1027. return dee;
  1028. }
  1029. static int
  1030. dirEntrySize(Source *s, ulong elem, ulong gen, uvlong *size)
  1031. {
  1032. Block *b;
  1033. ulong bn;
  1034. Entry e;
  1035. int epb;
  1036. epb = s->dsize/VtEntrySize;
  1037. bn = elem/epb;
  1038. elem -= bn*epb;
  1039. b = sourceBlock(s, bn, OReadOnly);
  1040. if(b == nil)
  1041. goto Err;
  1042. if(!entryUnpack(&e, b->data, elem))
  1043. goto Err;
  1044. /* hanging entries are returned as zero size */
  1045. if(!(e.flags & VtEntryActive) || e.gen != gen)
  1046. *size = 0;
  1047. else
  1048. *size = e.size;
  1049. blockPut(b);
  1050. return 1;
  1051. Err:
  1052. blockPut(b);
  1053. return 0;
  1054. }
  1055. static int
  1056. deeFill(DirEntryEnum *dee)
  1057. {
  1058. int i, n;
  1059. Source *meta, *source;
  1060. MetaBlock mb;
  1061. MetaEntry me;
  1062. File *f;
  1063. Block *b;
  1064. DirEntry *de;
  1065. /* clean up first */
  1066. for(i=dee->i; i<dee->n; i++)
  1067. deCleanup(dee->buf+i);
  1068. vtMemFree(dee->buf);
  1069. dee->buf = nil;
  1070. dee->i = 0;
  1071. dee->n = 0;
  1072. f = dee->file;
  1073. source = f->source;
  1074. meta = f->msource;
  1075. b = sourceBlock(meta, dee->boff, OReadOnly);
  1076. if(b == nil)
  1077. goto Err;
  1078. if(!mbUnpack(&mb, b->data, meta->dsize))
  1079. goto Err;
  1080. n = mb.nindex;
  1081. dee->buf = vtMemAlloc(n * sizeof(DirEntry));
  1082. for(i=0; i<n; i++){
  1083. de = dee->buf + i;
  1084. meUnpack(&me, &mb, i);
  1085. if(!deUnpack(de, &me))
  1086. goto Err;
  1087. dee->n++;
  1088. if(!(de->mode & ModeDir))
  1089. if(!dirEntrySize(source, de->entry, de->gen, &de->size))
  1090. goto Err;
  1091. }
  1092. dee->boff++;
  1093. blockPut(b);
  1094. return 1;
  1095. Err:
  1096. blockPut(b);
  1097. return 0;
  1098. }
  1099. int
  1100. deeRead(DirEntryEnum *dee, DirEntry *de)
  1101. {
  1102. int ret, didread;
  1103. File *f;
  1104. u32int nb;
  1105. f = dee->file;
  1106. if(!fileRLock(f))
  1107. return -1;
  1108. if(!sourceLock2(f->source, f->msource, OReadOnly)){
  1109. fileRUnlock(f);
  1110. return -1;
  1111. }
  1112. nb = (sourceGetSize(f->msource)+f->msource->dsize-1)/f->msource->dsize;
  1113. didread = 0;
  1114. while(dee->i >= dee->n){
  1115. if(dee->boff >= nb){
  1116. ret = 0;
  1117. goto Return;
  1118. }
  1119. didread = 1;
  1120. if(!deeFill(dee)){
  1121. ret = -1;
  1122. goto Return;
  1123. }
  1124. }
  1125. memmove(de, dee->buf + dee->i, sizeof(DirEntry));
  1126. dee->i++;
  1127. ret = 1;
  1128. Return:
  1129. sourceUnlock(f->source);
  1130. sourceUnlock(f->msource);
  1131. fileRUnlock(f);
  1132. if(didread)
  1133. fileRAccess(f);
  1134. return ret;
  1135. }
  1136. void
  1137. deeClose(DirEntryEnum *dee)
  1138. {
  1139. int i;
  1140. if(dee == nil)
  1141. return;
  1142. for(i=dee->i; i<dee->n; i++)
  1143. deCleanup(dee->buf+i);
  1144. vtMemFree(dee->buf);
  1145. fileDecRef(dee->file);
  1146. vtMemFree(dee);
  1147. }
  1148. /*
  1149. * caller must lock f->source and f->msource
  1150. * caller must NOT lock the source and msource
  1151. * referenced by dir.
  1152. */
  1153. static u32int
  1154. fileMetaAlloc(File *f, DirEntry *dir, u32int start)
  1155. {
  1156. u32int nb, bo;
  1157. Block *b, *bb;
  1158. MetaBlock mb;
  1159. int nn;
  1160. uchar *p;
  1161. int i, n, epb;
  1162. MetaEntry me;
  1163. Source *s, *ms;
  1164. s = f->source;
  1165. ms = f->msource;
  1166. n = deSize(dir);
  1167. nb = (sourceGetSize(ms)+ms->dsize-1)/ms->dsize;
  1168. b = nil;
  1169. if(start > nb)
  1170. start = nb;
  1171. for(bo=start; bo<nb; bo++){
  1172. b = sourceBlock(ms, bo, OReadWrite);
  1173. if(b == nil)
  1174. goto Err;
  1175. if(!mbUnpack(&mb, b->data, ms->dsize))
  1176. goto Err;
  1177. nn = (mb.maxsize*FullPercentage/100) - mb.size + mb.free;
  1178. if(n <= nn && mb.nindex < mb.maxindex)
  1179. break;
  1180. blockPut(b);
  1181. b = nil;
  1182. }
  1183. /* add block to meta file */
  1184. if(b == nil){
  1185. b = sourceBlock(ms, bo, OReadWrite);
  1186. if(b == nil)
  1187. goto Err;
  1188. sourceSetSize(ms, (nb+1)*ms->dsize);
  1189. mbInit(&mb, b->data, ms->dsize, ms->dsize/BytesPerEntry);
  1190. }
  1191. p = mbAlloc(&mb, n);
  1192. if(p == nil){
  1193. /* mbAlloc might have changed block */
  1194. mbPack(&mb);
  1195. blockDirty(b);
  1196. vtSetError(EBadMeta);
  1197. goto Err;
  1198. }
  1199. mbSearch(&mb, dir->elem, &i, &me);
  1200. assert(me.p == nil);
  1201. me.p = p;
  1202. me.size = n;
  1203. dePack(dir, &me);
  1204. mbInsert(&mb, i, &me);
  1205. mbPack(&mb);
  1206. /* meta block depends on super block for qid ... */
  1207. bb = cacheLocal(b->c, PartSuper, 0, OReadOnly);
  1208. blockDependency(b, bb, -1, nil, nil);
  1209. blockPut(bb);
  1210. /* ... and one or two dir entries */
  1211. epb = s->dsize/VtEntrySize;
  1212. bb = sourceBlock(s, dir->entry/epb, OReadOnly);
  1213. blockDependency(b, bb, -1, nil, nil);
  1214. blockPut(bb);
  1215. if(dir->mode & ModeDir){
  1216. bb = sourceBlock(s, dir->mentry/epb, OReadOnly);
  1217. blockDependency(b, bb, -1, nil, nil);
  1218. blockPut(bb);
  1219. }
  1220. blockDirty(b);
  1221. blockPut(b);
  1222. return bo;
  1223. Err:
  1224. blockPut(b);
  1225. return NilBlock;
  1226. }
  1227. static int
  1228. chkSource(File *f)
  1229. {
  1230. if(f->partial)
  1231. return 1;
  1232. if(f->source == nil || (f->dir.mode & ModeDir) && f->msource == nil){
  1233. vtSetError(ERemoved);
  1234. return 0;
  1235. }
  1236. return 1;
  1237. }
  1238. static int
  1239. fileRLock(File *f)
  1240. {
  1241. assert(!vtCanLock(f->fs->elk));
  1242. vtRLock(f->lk);
  1243. if(!chkSource(f)){
  1244. fileRUnlock(f);
  1245. return 0;
  1246. }
  1247. return 1;
  1248. }
  1249. static void
  1250. fileRUnlock(File *f)
  1251. {
  1252. vtRUnlock(f->lk);
  1253. }
  1254. static int
  1255. fileLock(File *f)
  1256. {
  1257. assert(!vtCanLock(f->fs->elk));
  1258. vtLock(f->lk);
  1259. if(!chkSource(f)){
  1260. fileUnlock(f);
  1261. return 0;
  1262. }
  1263. return 1;
  1264. }
  1265. static void
  1266. fileUnlock(File *f)
  1267. {
  1268. vtUnlock(f->lk);
  1269. }
  1270. /*
  1271. * f->source and f->msource must NOT be locked.
  1272. * fileMetaFlush locks the fileMeta and then the source (in fileMetaFlush2).
  1273. * We have to respect that ordering.
  1274. */
  1275. static void
  1276. fileMetaLock(File *f)
  1277. {
  1278. if(f->up == nil)
  1279. fprint(2, "f->elem = %s\n", f->dir.elem);
  1280. assert(f->up != nil);
  1281. assert(!vtCanLock(f->fs->elk));
  1282. vtLock(f->up->lk);
  1283. }
  1284. static void
  1285. fileMetaUnlock(File *f)
  1286. {
  1287. vtUnlock(f->up->lk);
  1288. }
  1289. /*
  1290. * f->source and f->msource must NOT be locked.
  1291. * see fileMetaLock.
  1292. */
  1293. static void
  1294. fileRAccess(File* f)
  1295. {
  1296. if(f->mode == OReadOnly)
  1297. return;
  1298. fileMetaLock(f);
  1299. f->dir.atime = time(0L);
  1300. f->dirty = 1;
  1301. fileMetaUnlock(f);
  1302. }
  1303. /*
  1304. * f->source and f->msource must NOT be locked.
  1305. * see fileMetaLock.
  1306. */
  1307. static void
  1308. fileWAccess(File* f, char *mid)
  1309. {
  1310. if(f->mode == OReadOnly)
  1311. return;
  1312. fileMetaLock(f);
  1313. f->dir.atime = f->dir.mtime = time(0L);
  1314. if(strcmp(f->dir.mid, mid) != 0){
  1315. vtMemFree(f->dir.mid);
  1316. f->dir.mid = vtStrDup(mid);
  1317. }
  1318. f->dir.mcount++;
  1319. f->dirty = 1;
  1320. fileMetaUnlock(f);
  1321. }
  1322. static void
  1323. markCopied(Block *b)
  1324. {
  1325. Block *lb;
  1326. Label l;
  1327. if(globalToLocal(b->score) == NilBlock)
  1328. return;
  1329. if(!(b->l.state & BsCopied)){
  1330. /*
  1331. * We need to record that there are now pointers in
  1332. * b that are not unique to b. We do this by marking
  1333. * b as copied. Since we don't return the label block,
  1334. * the caller can't get the dependencies right. So we have
  1335. * to flush the block ourselves. This is a rare occurrence.
  1336. */
  1337. l = b->l;
  1338. l.state |= BsCopied;
  1339. lb = _blockSetLabel(b, &l);
  1340. WriteAgain:
  1341. while(!blockWrite(lb)){
  1342. fprint(2, "getEntry: could not write label block\n");
  1343. sleep(10*1000);
  1344. }
  1345. while(lb->iostate != BioClean && lb->iostate != BioDirty){
  1346. assert(lb->iostate == BioWriting);
  1347. vtSleep(lb->ioready);
  1348. }
  1349. if(lb->iostate == BioDirty)
  1350. goto WriteAgain;
  1351. blockPut(lb);
  1352. }
  1353. }
  1354. static int
  1355. getEntry(Source *r, Entry *e, int mark)
  1356. {
  1357. Block *b;
  1358. if(r == nil){
  1359. memset(&e, 0, sizeof e);
  1360. return 1;
  1361. }
  1362. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadOnly);
  1363. if(b == nil)
  1364. return 0;
  1365. if(!entryUnpack(e, b->data, r->offset % r->epb)){
  1366. blockPut(b);
  1367. return 0;
  1368. }
  1369. if(mark)
  1370. markCopied(b);
  1371. blockPut(b);
  1372. return 1;
  1373. }
  1374. static int
  1375. setEntry(Source *r, Entry *e)
  1376. {
  1377. Block *b;
  1378. Entry oe;
  1379. b = cacheGlobal(r->fs->cache, r->score, BtDir, r->tag, OReadWrite);
  1380. if(0) fprint(2, "setEntry: b %#ux %d score=%V\n", b->addr, r->offset % r->epb, e->score);
  1381. if(b == nil)
  1382. return 0;
  1383. if(!entryUnpack(&oe, b->data, r->offset % r->epb)){
  1384. blockPut(b);
  1385. return 0;
  1386. }
  1387. e->gen = oe.gen;
  1388. entryPack(e, b->data, r->offset % r->epb);
  1389. /* BUG b should depend on the entry pointer */
  1390. markCopied(b);
  1391. blockDirty(b);
  1392. blockPut(b);
  1393. return 1;
  1394. }
  1395. /* assumes hold elk */
  1396. int
  1397. fileSnapshot(File *dst, File *src, u32int epoch, int doarchive)
  1398. {
  1399. Entry e, ee;
  1400. /* add link to snapshot */
  1401. if(!getEntry(src->source, &e, 1) || !getEntry(src->msource, &ee, 1))
  1402. return 0;
  1403. e.snap = epoch;
  1404. e.archive = doarchive;
  1405. ee.snap = epoch;
  1406. ee.archive = doarchive;
  1407. if(!setEntry(dst->source, &e) || !setEntry(dst->msource, &ee))
  1408. return 0;
  1409. return 1;
  1410. }
  1411. int
  1412. fileGetSources(File *f, Entry *e, Entry *ee, int mark)
  1413. {
  1414. if(!getEntry(f->source, e, mark)
  1415. || !getEntry(f->msource, ee, mark))
  1416. return 0;
  1417. return 1;
  1418. }
  1419. int
  1420. fileWalkSources(File *f)
  1421. {
  1422. if(f->mode == OReadOnly)
  1423. return 1;
  1424. if(!sourceLock2(f->source, f->msource, OReadWrite))
  1425. return 0;
  1426. sourceUnlock(f->source);
  1427. sourceUnlock(f->msource);
  1428. return 1;
  1429. }