devtinyfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * a pity the code isn't also tiny...
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "../port/error.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. enum{
  11. Qdir,
  12. Qmedium,
  13. Maxfs= 10, /* max file systems */
  14. Blen= 48, /* block length */
  15. Nlen= 28, /* name length */
  16. Dlen= Blen - 4,
  17. Tagdir= 'd',
  18. Tagdata= 'D',
  19. Tagend= 'e',
  20. Tagfree= 'f',
  21. Notapin= 0xffff,
  22. Notabno= 0xffff,
  23. Fcreating= 1,
  24. Frmonclose= 2
  25. };
  26. /* representation of a Tdir on medium */
  27. typedef struct Mdir Mdir;
  28. struct Mdir {
  29. uchar type;
  30. uchar bno[2];
  31. uchar pin[2];
  32. char name[Nlen];
  33. char pad[Blen - Nlen - 6];
  34. uchar sum;
  35. };
  36. /* representation of a Tdata/Tend on medium */
  37. typedef struct Mdata Mdata;
  38. struct Mdata {
  39. uchar type;
  40. uchar bno[2];
  41. uchar data[Dlen];
  42. uchar sum;
  43. };
  44. typedef struct Tfile Tfile;
  45. struct Tfile {
  46. int r;
  47. char name[NAMELEN];
  48. ushort bno;
  49. ushort dbno;
  50. ushort pin;
  51. uchar flag;
  52. ulong length;
  53. /* hint to avoid egregious reading */
  54. ushort fbno;
  55. ulong finger;
  56. };
  57. typedef struct Tfs Tfs;
  58. struct Tfs {
  59. QLock ql;
  60. int r;
  61. Chan *c;
  62. uchar *map;
  63. int nblocks;
  64. Tfile *f;
  65. int nf;
  66. int fsize;
  67. };
  68. struct {
  69. Tfs fs[Maxfs];
  70. } tinyfs;
  71. #define GETS(x) ((x)[0]|((x)[1]<<8))
  72. #define PUTS(x, v) {(x)[0] = (v);(x)[1] = ((v)>>8);}
  73. #define GETL(x) (GETS(x)|(GETS(x+2)<<16))
  74. #define PUTL(x, v) {PUTS(x, v);PUTS(x+2, (v)>>16)};
  75. static uchar
  76. checksum(uchar *p)
  77. {
  78. uchar *e;
  79. uchar s;
  80. s = 0;
  81. for(e = p + Blen; p < e; p++)
  82. s += *p;
  83. return s;
  84. }
  85. static void
  86. mapclr(Tfs *fs, ulong bno)
  87. {
  88. fs->map[bno>>3] &= ~(1<<(bno&7));
  89. }
  90. static void
  91. mapset(Tfs *fs, ulong bno)
  92. {
  93. fs->map[bno>>3] |= 1<<(bno&7);
  94. }
  95. static int
  96. isalloced(Tfs *fs, ulong bno)
  97. {
  98. return fs->map[bno>>3] & (1<<(bno&7));
  99. }
  100. static int
  101. mapalloc(Tfs *fs)
  102. {
  103. int i, j, lim;
  104. uchar x;
  105. lim = (fs->nblocks + 8 - 1)/8;
  106. for(i = 0; i < lim; i++){
  107. x = fs->map[i];
  108. if(x == 0xff)
  109. continue;
  110. for(j = 0; j < 8; j++)
  111. if((x & (1<<j)) == 0){
  112. fs->map[i] = x|(1<<j);
  113. return i*8 + j;
  114. }
  115. }
  116. return Notabno;
  117. }
  118. static Mdir*
  119. validdir(Tfs *fs, uchar *p)
  120. {
  121. Mdir *md;
  122. ulong x;
  123. if(checksum(p) != 0)
  124. return 0;
  125. if(p[0] != Tagdir)
  126. return 0;
  127. md = (Mdir*)p;
  128. x = GETS(md->bno);
  129. if(x >= fs->nblocks)
  130. return 0;
  131. return md;
  132. }
  133. static Mdata*
  134. validdata(Tfs *fs, uchar *p, int *lenp)
  135. {
  136. Mdata *md;
  137. ulong x;
  138. if(checksum(p) != 0)
  139. return 0;
  140. md = (Mdata*)p;
  141. switch(md->type){
  142. case Tagdata:
  143. x = GETS(md->bno);
  144. if(x >= fs->nblocks)
  145. return 0;
  146. if(lenp)
  147. *lenp = Dlen;
  148. break;
  149. case Tagend:
  150. x = GETS(md->bno);
  151. if(x > Dlen)
  152. return 0;
  153. if(lenp)
  154. *lenp = x;
  155. break;
  156. default:
  157. return 0;
  158. }
  159. return md;
  160. }
  161. static Mdata*
  162. readdata(Tfs *fs, ulong bno, uchar *buf, int *lenp)
  163. {
  164. if(bno >= fs->nblocks)
  165. return 0;
  166. if(devtab[fs->c->type]->read(fs->c, buf, Blen, Blen*bno) != Blen)
  167. error(Eio);
  168. return validdata(fs, buf, lenp);
  169. }
  170. static void
  171. writedata(Tfs *fs, ulong bno, ulong next, uchar *buf, int len, int last)
  172. {
  173. Mdata md;
  174. if(bno >= fs->nblocks)
  175. error(Eio);
  176. if(len > Dlen)
  177. len = Dlen;
  178. if(len < 0)
  179. error(Eio);
  180. memset(&md, 0, sizeof(md));
  181. if(last){
  182. md.type = Tagend;
  183. PUTS(md.bno, len);
  184. } else {
  185. md.type = Tagdata;
  186. PUTS(md.bno, next);
  187. }
  188. memmove(md.data, buf, len);
  189. md.sum = 0 - checksum((uchar*)&md);
  190. if(devtab[fs->c->type]->write(fs->c, &md, Blen, Blen*bno) != Blen)
  191. error(Eio);
  192. }
  193. static void
  194. writedir(Tfs *fs, Tfile *f)
  195. {
  196. Mdir *md;
  197. uchar buf[Blen];
  198. if(f->bno == Notabno)
  199. return;
  200. md = (Mdir*)buf;
  201. memset(buf, 0, Blen);
  202. md->type = Tagdir;
  203. strncpy(md->name, f->name, sizeof(md->name)-1);
  204. PUTS(md->bno, f->dbno);
  205. PUTS(md->pin, f->pin);
  206. md->sum = 0 - checksum(buf);
  207. if(devtab[fs->c->type]->write(fs->c, buf, Blen, Blen*f->bno) != Blen)
  208. error(Eio);
  209. }
  210. static void
  211. freeblocks(Tfs *fs, ulong bno, ulong bend)
  212. {
  213. uchar buf[Blen];
  214. Mdata *md;
  215. if(waserror())
  216. return;
  217. while(bno != bend && bno != Notabno){
  218. mapclr(fs, bno);
  219. if(devtab[fs->c->type]->read(fs->c, buf, Blen, Blen*bno) != Blen)
  220. break;
  221. md = validdata(fs, buf, 0);
  222. if(md == 0)
  223. break;
  224. if(md->type == Tagend)
  225. break;
  226. bno = GETS(md->bno);
  227. }
  228. poperror();
  229. }
  230. static void
  231. freefile(Tfs *fs, Tfile *f, ulong bend)
  232. {
  233. uchar buf[Blen];
  234. /* remove blocks from map */
  235. freeblocks(fs, f->dbno, bend);
  236. /* change file type to free on medium */
  237. if(f->bno != Notabno){
  238. memset(buf, 0x55, Blen);
  239. devtab[fs->c->type]->write(fs->c, buf, Blen, Blen*f->bno);
  240. mapclr(fs, f->bno);
  241. }
  242. /* forget we ever knew about it */
  243. memset(f, 0, sizeof(*f));
  244. }
  245. static void
  246. expand(Tfs *fs)
  247. {
  248. Tfile *f;
  249. fs->fsize += 8;
  250. f = malloc(fs->fsize*sizeof(*f));
  251. if(fs->f){
  252. memmove(f, fs->f, fs->nf*sizeof(*f));
  253. free(fs->f);
  254. }
  255. fs->f = f;
  256. }
  257. static Tfile*
  258. newfile(Tfs *fs, char *name)
  259. {
  260. int i;
  261. volatile struct {
  262. Tfile *f;
  263. Tfs *fs;
  264. } rock;
  265. /* find free entry in file table */
  266. rock.f = 0;
  267. rock.fs = fs;
  268. for(;;) {
  269. for(i = 0; i < rock.fs->fsize; i++){
  270. rock.f = &rock.fs->f[i];
  271. if(rock.f->name[0] == 0){
  272. strncpy(rock.f->name, name, sizeof(rock.f->name)-1);
  273. break;
  274. }
  275. }
  276. if(i < rock.fs->fsize){
  277. if(i >= rock.fs->nf)
  278. rock.fs->nf = i+1;
  279. break;
  280. }
  281. expand(rock.fs);
  282. }
  283. rock.f->flag = Fcreating;
  284. rock.f->dbno = Notabno;
  285. rock.f->bno = mapalloc(rock.fs);
  286. rock.f->fbno = Notabno;
  287. rock.f->r = 1;
  288. rock.f->pin = Notapin; // what is a pin??
  289. /* write directory block */
  290. if(waserror()){
  291. freefile(rock.fs, rock.f, Notabno);
  292. nexterror();
  293. }
  294. if(rock.f->bno == Notabno)
  295. error("out of space");
  296. writedir(rock.fs, rock.f);
  297. poperror();
  298. return rock.f;
  299. }
  300. /*
  301. * Read the whole medium and build a file table and used
  302. * block bitmap. Inconsistent files are purged. The medium
  303. * had better be small or this could take a while.
  304. */
  305. static void
  306. tfsinit(Tfs *fs)
  307. {
  308. char dbuf[DIRLEN];
  309. Dir d;
  310. uchar buf[Blen];
  311. ulong x, bno;
  312. int n, done;
  313. Tfile *f;
  314. Mdir *mdir;
  315. Mdata *mdata;
  316. devtab[fs->c->type]->stat(fs->c, dbuf);
  317. convM2D(dbuf, &d);
  318. fs->nblocks = d.length/Blen;
  319. if(fs->nblocks < 3)
  320. error("tinyfs medium too small");
  321. /* bitmap for block usage */
  322. x = (fs->nblocks + 8 - 1)/8;
  323. fs->map = malloc(x);
  324. memset(fs->map, 0x0, x);
  325. for(bno = fs->nblocks; bno < x*8; bno++)
  326. mapset(fs, bno);
  327. /* find files */
  328. for(bno = 0; bno < fs->nblocks; bno++){
  329. n = devtab[fs->c->type]->read(fs->c, buf, Blen, Blen*bno);
  330. if(n != Blen)
  331. break;
  332. mdir = validdir(fs, buf);
  333. if(mdir == 0)
  334. continue;
  335. if(fs->nf >= fs->fsize)
  336. expand(fs);
  337. f = &fs->f[fs->nf++];
  338. x = GETS(mdir->bno);
  339. mapset(fs, bno);
  340. strncpy(f->name, mdir->name, sizeof(f->name));
  341. f->pin = GETS(mdir->pin);
  342. f->bno = bno;
  343. f->dbno = x;
  344. f->fbno = Notabno;
  345. }
  346. /* follow files */
  347. for(f = fs->f; f < &(fs->f[fs->nf]); f++){
  348. bno = f->dbno;
  349. for(done = 0; !done;) {
  350. if(isalloced(fs, bno)){
  351. freefile(fs, f, bno);
  352. break;
  353. }
  354. n = devtab[fs->c->type]->read(fs->c, buf, Blen, Blen*bno);
  355. if(n != Blen){
  356. freefile(fs, f, bno);
  357. break;
  358. }
  359. mdata = validdata(fs, buf, 0);
  360. if(mdata == 0){
  361. freefile(fs, f, bno);
  362. break;
  363. }
  364. mapset(fs, bno);
  365. switch(mdata->type){
  366. case Tagdata:
  367. bno = GETS(mdata->bno);
  368. f->length += Dlen;
  369. break;
  370. case Tagend:
  371. f->length += GETS(mdata->bno);
  372. done = 1;
  373. break;
  374. }
  375. if(done)
  376. f->flag &= ~Fcreating;
  377. }
  378. }
  379. }
  380. /*
  381. * single directory
  382. */
  383. static int
  384. tinyfsgen(Chan *c, Dirtab *tab, int ntab, int i, Dir *dp)
  385. {
  386. Tfs *fs;
  387. Tfile *f;
  388. Qid qid;
  389. USED(ntab);
  390. USED(tab);
  391. qid.vers = 0;
  392. fs = &tinyfs.fs[c->dev];
  393. if(i >= fs->nf)
  394. return -1;
  395. if(i == DEVDOTDOT){
  396. qid.path = CHDIR;
  397. devdir(c, qid, ".", 0, eve, 0555, dp);
  398. return 1;
  399. }
  400. f = &fs->f[i];
  401. if(f->name[0] == 0)
  402. return 0;
  403. qid.path = i;
  404. devdir(c, qid, f->name, f->length, eve, 0775, dp);
  405. return 1;
  406. }
  407. static void
  408. tinyfsinit(void)
  409. {
  410. if(Nlen > NAMELEN)
  411. panic("tinyfsinit");
  412. }
  413. /*
  414. * specifier is an open file descriptor
  415. */
  416. static Chan*
  417. tinyfsattach(char *spec)
  418. {
  419. Tfs *fs;
  420. Chan *c;
  421. volatile struct { Chan *cc; } rock;
  422. int i;
  423. char buf[NAMELEN*2];
  424. snprint(buf, sizeof(buf), "/dev/%s", spec);
  425. rock.cc = namec(buf, Aopen, ORDWR, 0);
  426. if(waserror()){
  427. cclose(rock.cc);
  428. nexterror();
  429. }
  430. fs = 0;
  431. for(i = 0; i < Maxfs; i++){
  432. fs = &tinyfs.fs[i];
  433. qlock(&fs->ql);
  434. if(fs->r && eqchan(rock.cc, fs->c, 1))
  435. break;
  436. qunlock(&fs->ql);
  437. }
  438. if(i < Maxfs){
  439. fs->r++;
  440. qunlock(&fs->ql);
  441. cclose(rock.cc);
  442. } else {
  443. for(fs = tinyfs.fs; fs < &tinyfs.fs[Maxfs]; fs++){
  444. qlock(&fs->ql);
  445. if(fs->r == 0)
  446. break;
  447. qunlock(&fs->ql);
  448. }
  449. if(fs == &tinyfs.fs[Maxfs])
  450. error("too many tinyfs's");
  451. fs->c = rock.cc;
  452. fs->r = 1;
  453. fs->f = 0;
  454. fs->nf = 0;
  455. fs->fsize = 0;
  456. tfsinit(fs);
  457. qunlock(&fs->ql);
  458. }
  459. poperror();
  460. c = devattach('F', spec);
  461. c->dev = fs - tinyfs.fs;
  462. c->qid.path = CHDIR;
  463. c->qid.vers = 0;
  464. return c;
  465. }
  466. static Chan*
  467. tinyfsclone(Chan *c, Chan *nc)
  468. {
  469. Tfs *fs;
  470. fs = &tinyfs.fs[c->dev];
  471. qlock(&fs->ql);
  472. fs->r++;
  473. qunlock(&fs->ql);
  474. return devclone(c, nc);
  475. }
  476. static int
  477. tinyfswalk(Chan *c, char *name)
  478. {
  479. int n;
  480. Tfs *fs;
  481. fs = &tinyfs.fs[c->dev];
  482. qlock(&fs->ql);
  483. n = devwalk(c, name, 0, 0, tinyfsgen);
  484. if(n != 0 && c->qid.path != CHDIR){
  485. fs = &tinyfs.fs[c->dev];
  486. fs->f[c->qid.path].r++;
  487. }
  488. qunlock(&fs->ql);
  489. return n;
  490. }
  491. static void
  492. tinyfsstat(Chan *c, char *db)
  493. {
  494. devstat(c, db, 0, 0, tinyfsgen);
  495. }
  496. static Chan*
  497. tinyfsopen(Chan *c, int omode)
  498. {
  499. Tfile *f;
  500. volatile struct { Tfs *fs; } rock;
  501. rock.fs = &tinyfs.fs[c->dev];
  502. if(c->qid.path & CHDIR){
  503. if(omode != OREAD)
  504. error(Eperm);
  505. } else {
  506. qlock(&rock.fs->ql);
  507. if(waserror()){
  508. qunlock(&rock.fs->ql);
  509. nexterror();
  510. }
  511. switch(omode){
  512. case OTRUNC|ORDWR:
  513. case OTRUNC|OWRITE:
  514. f = newfile(rock.fs, rock.fs->f[c->qid.path].name);
  515. rock.fs->f[c->qid.path].r--;
  516. c->qid.path = f - rock.fs->f;
  517. break;
  518. case OREAD:
  519. case OEXEC:
  520. break;
  521. default:
  522. error(Eperm);
  523. }
  524. qunlock(&rock.fs->ql);
  525. poperror();
  526. }
  527. return devopen(c, omode, 0, 0, tinyfsgen);
  528. }
  529. static void
  530. tinyfscreate(Chan *c, char *name, int omode, ulong perm)
  531. {
  532. volatile struct { Tfs *fs; } rock;
  533. Tfile *f;
  534. USED(perm);
  535. rock.fs = &tinyfs.fs[c->dev];
  536. qlock(&rock.fs->ql);
  537. if(waserror()){
  538. qunlock(&rock.fs->ql);
  539. nexterror();
  540. }
  541. f = newfile(rock.fs, name);
  542. qunlock(&rock.fs->ql);
  543. poperror();
  544. c->qid.path = f - rock.fs->f;
  545. c->qid.vers = 0;
  546. c->mode = openmode(omode);
  547. }
  548. static void
  549. tinyfsremove(Chan *c)
  550. {
  551. Tfs *fs;
  552. Tfile *f;
  553. if(c->qid.path == CHDIR)
  554. error(Eperm);
  555. fs = &tinyfs.fs[c->dev];
  556. f = &fs->f[c->qid.path];
  557. qlock(&fs->ql);
  558. freefile(fs, f, Notabno);
  559. qunlock(&fs->ql);
  560. }
  561. static void
  562. tinyfsclose(Chan *c)
  563. {
  564. volatile struct { Tfs *fs; } rock;
  565. Tfile *f, *nf;
  566. int i;
  567. rock.fs = &tinyfs.fs[c->dev];
  568. qlock(&rock.fs->ql);
  569. /* dereference file and remove old versions */
  570. if(!waserror()){
  571. if(c->qid.path != CHDIR){
  572. f = &rock.fs->f[c->qid.path];
  573. f->r--;
  574. if(f->r == 0){
  575. if(f->flag & Frmonclose)
  576. freefile(rock.fs, f, Notabno);
  577. else if(f->flag & Fcreating){
  578. /* remove all other files with this name */
  579. for(i = 0; i < rock.fs->fsize; i++){
  580. nf = &rock.fs->f[i];
  581. if(f == nf)
  582. continue;
  583. if(strcmp(nf->name, f->name) == 0){
  584. if(nf->r)
  585. nf->flag |= Frmonclose;
  586. else
  587. freefile(rock.fs, nf, Notabno);
  588. }
  589. }
  590. f->flag &= ~Fcreating;
  591. }
  592. }
  593. }
  594. poperror();
  595. }
  596. /* dereference rock.fs and remove on zero refs */
  597. rock.fs->r--;
  598. if(rock.fs->r == 0){
  599. if(rock.fs->f)
  600. free(rock.fs->f);
  601. rock.fs->f = 0;
  602. rock.fs->nf = 0;
  603. rock.fs->fsize = 0;
  604. if(rock.fs->map)
  605. free(rock.fs->map);
  606. rock.fs->map = 0;
  607. cclose(rock.fs->c);
  608. rock.fs->c = 0;
  609. }
  610. qunlock(&rock.fs->ql);
  611. }
  612. static long
  613. tinyfsread(Chan *c, void *a, long n, vlong offset)
  614. {
  615. volatile struct { Tfs *fs; } rock;
  616. Tfile *f;
  617. int sofar, i, off;
  618. ulong bno;
  619. Mdata *md;
  620. uchar buf[Blen];
  621. uchar *p;
  622. if(c->qid.path & CHDIR)
  623. return devdirread(c, a, n, 0, 0, tinyfsgen);
  624. p = a;
  625. rock.fs = &tinyfs.fs[c->dev];
  626. f = &rock.fs->f[c->qid.path];
  627. if(offset >= f->length)
  628. return 0;
  629. qlock(&rock.fs->ql);
  630. if(waserror()){
  631. qunlock(&rock.fs->ql);
  632. nexterror();
  633. }
  634. if(n + offset >= f->length)
  635. n = f->length - offset;
  636. /* walk to starting data block */
  637. if(0 && f->finger <= offset && f->fbno != Notabno){
  638. sofar = f->finger;
  639. bno = f->fbno;
  640. } else {
  641. sofar = 0;
  642. bno = f->dbno;
  643. }
  644. for(; sofar + Dlen <= offset; sofar += Dlen){
  645. md = readdata(rock.fs, bno, buf, 0);
  646. if(md == 0)
  647. error(Eio);
  648. bno = GETS(md->bno);
  649. }
  650. /* read data */
  651. off = offset%Dlen;
  652. offset -= off;
  653. for(sofar = 0; sofar < n; sofar += i){
  654. md = readdata(rock.fs, bno, buf, &i);
  655. if(md == 0)
  656. error(Eio);
  657. /* update finger for successful read */
  658. f->finger = offset;
  659. f->fbno = bno;
  660. offset += Dlen;
  661. i -= off;
  662. if(i > n - sofar)
  663. i = n - sofar;
  664. memmove(p, md->data+off, i);
  665. p += i;
  666. bno = GETS(md->bno);
  667. off = 0;
  668. }
  669. qunlock(&rock.fs->ql);
  670. poperror();
  671. return sofar;
  672. }
  673. /*
  674. * if we get a write error in this routine, blocks will
  675. * be lost. They should be recovered next fsinit.
  676. */
  677. static long
  678. tinyfswrite(Chan *c, void *a, long n, vlong offset)
  679. {
  680. Tfile *f;
  681. int last, next, i, finger, off, used;
  682. ulong bno, fbno;
  683. Mdata *md;
  684. uchar buf[Blen];
  685. uchar *p;
  686. volatile struct {
  687. Tfs *fs;
  688. ulong dbno;
  689. } rock;
  690. if(c->qid.path & CHDIR)
  691. error(Eperm);
  692. if(n == 0)
  693. return 0;
  694. p = a;
  695. rock.fs = &tinyfs.fs[c->dev];
  696. f = &rock.fs->f[c->qid.path];
  697. qlock(&rock.fs->ql);
  698. rock.dbno = Notabno;
  699. if(waserror()){
  700. freeblocks(rock.fs, rock.dbno, Notabno);
  701. qunlock(&rock.fs->ql);
  702. nexterror();
  703. }
  704. /* files are append only, anything else is illegal */
  705. if(offset != f->length)
  706. error("append only");
  707. /* write blocks backwards */
  708. p += n;
  709. last = offset + n;
  710. fbno = Notabno;
  711. finger = 0;
  712. off = offset; /* so we have something signed to compare against */
  713. for(next = ((last-1)/Dlen)*Dlen; next >= off; next -= Dlen){
  714. bno = mapalloc(rock.fs);
  715. if(bno == Notabno)
  716. error("out of space");
  717. i = last - next;
  718. p -= i;
  719. if(last == n+offset){
  720. writedata(rock.fs, bno, rock.dbno, p, i, 1);
  721. finger = next; /* remember for later */
  722. fbno = bno;
  723. } else {
  724. writedata(rock.fs, bno, rock.dbno, p, i, 0);
  725. }
  726. rock.dbno = bno;
  727. last = next;
  728. }
  729. /* walk to last data block */
  730. md = (Mdata*)buf;
  731. if(0 && f->finger < offset && f->fbno != Notabno){
  732. next = f->finger;
  733. bno = f->fbno;
  734. } else {
  735. next = 0;
  736. bno = f->dbno;
  737. }
  738. used = 0;
  739. while(bno != Notabno){
  740. md = readdata(rock.fs, bno, buf, &used);
  741. if(md == 0)
  742. error(Eio);
  743. if(md->type == Tagend){
  744. if(next + Dlen < offset)
  745. panic("devtinyfs1");
  746. break;
  747. }
  748. next += Dlen;
  749. if(next > offset)
  750. panic("devtinyfs1");
  751. bno = GETS(md->bno);
  752. }
  753. /* point to new blocks */
  754. if(offset == 0){
  755. /* first block in a file */
  756. f->dbno = rock.dbno;
  757. writedir(rock.fs, f);
  758. } else {
  759. /* updating a current block */
  760. i = last - offset;
  761. if(i > 0){
  762. p -= i;
  763. memmove(md->data + used, p, i);
  764. used += i;
  765. }
  766. writedata(rock.fs, bno, rock.dbno, md->data, used, last == n+offset);
  767. }
  768. f->length += n;
  769. /* update finger */
  770. if(fbno != Notabno){
  771. f->finger = finger;
  772. f->fbno = fbno;
  773. }
  774. poperror();
  775. qunlock(&rock.fs->ql);
  776. return n;
  777. }
  778. Dev tinyfsdevtab = {
  779. 'F',
  780. "tinyfs",
  781. devreset,
  782. tinyfsinit,
  783. tinyfsattach,
  784. tinyfsclone,
  785. tinyfswalk,
  786. tinyfsstat,
  787. tinyfsopen,
  788. tinyfscreate,
  789. tinyfsclose,
  790. tinyfsread,
  791. devbread,
  792. tinyfswrite,
  793. devbwrite,
  794. tinyfsremove,
  795. devwstat,
  796. };