file.c 27 KB

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