file.c 29 KB

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