file.c 31 KB

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