file.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 %ud -> %ud\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 %#ux %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. }