oramfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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 <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <fcall.h>
  13. #include "bzfs.h"
  14. /*
  15. * Rather than reading /adm/users, which is a lot of work for
  16. * a toy program, we assume all groups have the form
  17. * NNN:user:user:
  18. * meaning that each user is the leader of his own group.
  19. */
  20. enum
  21. {
  22. OPERM = 0x3, /* mask of all permission types in open mode */
  23. Nram = 512,
  24. Maxsize = 512*1024*1024,
  25. Maxfdata = 8192,
  26. };
  27. typedef struct Fid Fid;
  28. typedef struct Ram Ram;
  29. struct Fid
  30. {
  31. short busy;
  32. short open;
  33. short rclose;
  34. int fid;
  35. Fid *next;
  36. char *user;
  37. Ram *ram;
  38. };
  39. struct Ram
  40. {
  41. short busy;
  42. short open;
  43. long parent; /* index in Ram array */
  44. Qid qid;
  45. long perm;
  46. char *name;
  47. uint32_t atime;
  48. uint32_t mtime;
  49. char *user;
  50. char *group;
  51. char *muid;
  52. char *data;
  53. long ndata;
  54. };
  55. enum
  56. {
  57. Pexec = 1,
  58. Pwrite = 2,
  59. Pread = 4,
  60. Pother = 1,
  61. Pgroup = 8,
  62. Powner = 64,
  63. };
  64. uint32_t path; /* incremented for each new file */
  65. Fid *fids;
  66. Ram ram[Nram];
  67. int nram;
  68. int mfd[2];
  69. char *user;
  70. uchar mdata[IOHDRSZ+Maxfdata];
  71. uchar rdata[Maxfdata]; /* buffer for data in reply */
  72. uchar statbuf[STATMAX];
  73. Fcall thdr;
  74. Fcall rhdr;
  75. int messagesize = sizeof mdata;
  76. Fid * newfid(int);
  77. uint ramstat(Ram*, uchar*, uint);
  78. void io(void);
  79. void *erealloc(void*, uint32_t);
  80. void *emalloc(uint32_t);
  81. char *estrdup(char*);
  82. void ramfsusage(void);
  83. int perm(Fid*, Ram*, int);
  84. char *atom(char*);
  85. char *rflush(Fid*), *rversion(Fid*), *rauth(Fid*),
  86. *rattach(Fid*), *rwalk(Fid*),
  87. *ropen(Fid*), *rcreate(Fid*),
  88. *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
  89. *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
  90. char *(*fcalls[])(Fid*) = {
  91. [Tversion] rversion,
  92. [Tflush] rflush,
  93. [Tauth] rauth,
  94. [Tattach] rattach,
  95. [Twalk] rwalk,
  96. [Topen] ropen,
  97. [Tcreate] rcreate,
  98. [Tread] rread,
  99. [Twrite] rwrite,
  100. [Tclunk] rclunk,
  101. [Tremove] rremove,
  102. [Tstat] rstat,
  103. [Twstat] rwstat,
  104. };
  105. char Eperm[] = "permission denied";
  106. char Enotdir[] = "not a directory";
  107. char Enoauth[] = "no authentication in ramfs";
  108. char Enotexist[] = "file does not exist";
  109. char Einuse[] = "file in use";
  110. char Eexist[] = "file exists";
  111. char Eisdir[] = "file is a directory";
  112. char Enotowner[] = "not owner";
  113. char Eisopen[] = "file already open for I/O";
  114. char Excl[] = "exclusive use file already open";
  115. char Ename[] = "illegal name";
  116. char Eversion[] = "unknown 9P version";
  117. int debug;
  118. void
  119. notifyf(void *a, char *s)
  120. {
  121. USED(a);
  122. if(strncmp(s, "interrupt", 9) == 0)
  123. noted(NCONT);
  124. noted(NDFLT);
  125. }
  126. void
  127. ramfsmain(int argc, char *argv[])
  128. {
  129. Ram *r;
  130. char *defmnt;
  131. int p[2];
  132. char buf[32];
  133. int fd, srvfd;
  134. int stdio = 0;
  135. srvfd = -1;
  136. defmnt = "/tmp";
  137. ARGBEGIN{
  138. case 'D':
  139. debug = 1;
  140. break;
  141. case 'i': /* this is DIFFERENT from normal ramfs; use 1 for both for kernel */
  142. defmnt = 0;
  143. stdio = 1;
  144. srvfd = 0;
  145. mfd[0] = 1;
  146. mfd[1] = 1;
  147. break;
  148. case 's':
  149. defmnt = 0;
  150. break;
  151. case 'm':
  152. defmnt = ARGF();
  153. break;
  154. default:
  155. ramfsusage();
  156. }ARGEND
  157. if(!stdio){
  158. if(pipe(p) < 0)
  159. error("pipe failed");
  160. srvfd = p[1];
  161. mfd[0] = p[0];
  162. mfd[1] = p[0];
  163. if(defmnt == 0){
  164. fd = create("#s/ramfs", OWRITE, 0666);
  165. if(fd < 0)
  166. error("create of /srv/ramfs failed");
  167. sprint(buf, "%d", p[1]);
  168. if(write(fd, buf, strlen(buf)) < 0)
  169. error("writing /srv/ramfs");
  170. }
  171. }
  172. user = atom(getuser());
  173. notify(notifyf);
  174. nram = 1;
  175. r = &ram[0];
  176. r->busy = 1;
  177. r->data = 0;
  178. r->ndata = 0;
  179. r->perm = DMDIR | 0775;
  180. r->qid.type = QTDIR;
  181. r->qid.path = 0LL;
  182. r->qid.vers = 0;
  183. r->parent = 0;
  184. r->user = user;
  185. r->group = user;
  186. r->muid = user;
  187. r->atime = time(0);
  188. r->mtime = r->atime;
  189. r->name = estrdup(".");
  190. if(debug)
  191. fmtinstall('F', fcallfmt);
  192. switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
  193. case -1:
  194. error("fork");
  195. case 0:
  196. close(srvfd);
  197. io();
  198. break;
  199. default:
  200. close(mfd[0]); /* don't deadlock if child fails */
  201. if(defmnt && mount(srvfd, -1, defmnt, MREPL|MCREATE, "") < 0)
  202. error("mount failed: %r");
  203. }
  204. }
  205. char*
  206. rversion(Fid*)
  207. {
  208. Fid *f;
  209. for(f = fids; f; f = f->next)
  210. if(f->busy)
  211. rclunk(f);
  212. if(thdr.msize > sizeof mdata)
  213. rhdr.msize = sizeof mdata;
  214. else
  215. rhdr.msize = thdr.msize;
  216. messagesize = rhdr.msize;
  217. if(strncmp(thdr.version, "9P2000", 6) != 0)
  218. return Eversion;
  219. rhdr.version = "9P2000";
  220. return 0;
  221. }
  222. char*
  223. rauth(Fid*)
  224. {
  225. return "ramfs: no authentication required";
  226. }
  227. char*
  228. rflush(Fid *f)
  229. {
  230. USED(f);
  231. return 0;
  232. }
  233. char*
  234. rattach(Fid *f)
  235. {
  236. /* no authentication! */
  237. f->busy = 1;
  238. f->rclose = 0;
  239. f->ram = &ram[0];
  240. rhdr.qid = f->ram->qid;
  241. if(thdr.uname[0])
  242. f->user = atom(thdr.uname);
  243. else
  244. f->user = atom("none");
  245. if(strcmp(user, "none") == 0)
  246. user = f->user;
  247. return 0;
  248. }
  249. char*
  250. clone(Fid *f, Fid **nf)
  251. {
  252. if(f->open)
  253. return Eisopen;
  254. if(f->ram->busy == 0)
  255. return Enotexist;
  256. *nf = newfid(thdr.newfid);
  257. (*nf)->busy = 1;
  258. (*nf)->open = 0;
  259. (*nf)->rclose = 0;
  260. (*nf)->ram = f->ram;
  261. (*nf)->user = f->user; /* no ref count; the leakage is minor */
  262. return 0;
  263. }
  264. char*
  265. rwalk(Fid *f)
  266. {
  267. Ram *r, *fram;
  268. char *name;
  269. Ram *parent;
  270. Fid *nf;
  271. char *err;
  272. uint32_t t;
  273. int i;
  274. err = nil;
  275. nf = nil;
  276. rhdr.nwqid = 0;
  277. if(rhdr.newfid != rhdr.fid){
  278. err = clone(f, &nf);
  279. if(err)
  280. return err;
  281. f = nf; /* walk the new fid */
  282. }
  283. fram = f->ram;
  284. if(thdr.nwname > 0){
  285. t = time(0);
  286. for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
  287. if((fram->qid.type & QTDIR) == 0){
  288. err = Enotdir;
  289. break;
  290. }
  291. if(fram->busy == 0){
  292. err = Enotexist;
  293. break;
  294. }
  295. fram->atime = t;
  296. name = thdr.wname[i];
  297. if(strcmp(name, ".") == 0){
  298. Found:
  299. rhdr.nwqid++;
  300. rhdr.wqid[i] = fram->qid;
  301. continue;
  302. }
  303. parent = &ram[fram->parent];
  304. #ifdef CHECKS
  305. if(!perm(f, parent, Pexec)){
  306. err = Eperm;
  307. break;
  308. }
  309. #endif
  310. if(strcmp(name, "..") == 0){
  311. fram = parent;
  312. goto Found;
  313. }
  314. for(r=ram; r < &ram[nram]; r++)
  315. if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
  316. fram = r;
  317. goto Found;
  318. }
  319. break;
  320. }
  321. if(i==0 && err == nil)
  322. err = Enotexist;
  323. }
  324. if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
  325. /* clunk the new fid, which is the one we walked */
  326. f->busy = 0;
  327. f->ram = nil;
  328. }
  329. if(rhdr.nwqid == thdr.nwname) /* update the fid after a successful walk */
  330. f->ram = fram;
  331. return err;
  332. }
  333. char *
  334. ropen(Fid *f)
  335. {
  336. Ram *r;
  337. int mode, trunc;
  338. if(f->open)
  339. return Eisopen;
  340. r = f->ram;
  341. if(r->busy == 0)
  342. return Enotexist;
  343. if(r->perm & DMEXCL)
  344. if(r->open)
  345. return Excl;
  346. mode = thdr.mode;
  347. if(r->qid.type & QTDIR){
  348. if(mode != OREAD)
  349. return Eperm;
  350. rhdr.qid = r->qid;
  351. return 0;
  352. }
  353. if(mode & ORCLOSE){
  354. /* can't remove root; must be able to write parent */
  355. if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
  356. return Eperm;
  357. f->rclose = 1;
  358. }
  359. trunc = mode & OTRUNC;
  360. mode &= OPERM;
  361. if(mode==OWRITE || mode==ORDWR || trunc)
  362. if(!perm(f, r, Pwrite))
  363. return Eperm;
  364. if(mode==OREAD || mode==ORDWR)
  365. if(!perm(f, r, Pread))
  366. return Eperm;
  367. if(mode==OEXEC)
  368. if(!perm(f, r, Pexec))
  369. return Eperm;
  370. if(trunc && (r->perm&DMAPPEND)==0){
  371. r->ndata = 0;
  372. if(r->data)
  373. free(r->data);
  374. r->data = 0;
  375. r->qid.vers++;
  376. }
  377. rhdr.qid = r->qid;
  378. rhdr.iounit = messagesize-IOHDRSZ;
  379. f->open = 1;
  380. r->open++;
  381. return 0;
  382. }
  383. char *
  384. rcreate(Fid *f)
  385. {
  386. Ram *r;
  387. char *name;
  388. long parent, prm;
  389. if(f->open)
  390. return Eisopen;
  391. if(f->ram->busy == 0)
  392. return Enotexist;
  393. parent = f->ram - ram;
  394. if((f->ram->qid.type&QTDIR) == 0)
  395. return Enotdir;
  396. /* must be able to write parent */
  397. #ifdef CHECKS
  398. if(!perm(f, f->ram, Pwrite))
  399. return Eperm;
  400. #endif
  401. prm = thdr.perm;
  402. name = thdr.name;
  403. if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
  404. return Ename;
  405. for(r=ram; r<&ram[nram]; r++)
  406. if(r->busy && parent==r->parent)
  407. if(strcmp((char*)name, r->name)==0)
  408. return Einuse;
  409. for(r=ram; r->busy; r++)
  410. if(r == &ram[Nram-1])
  411. return "no free ram resources";
  412. r->busy = 1;
  413. r->qid.path = ++path;
  414. r->qid.vers = 0;
  415. if(prm & DMDIR)
  416. r->qid.type |= QTDIR;
  417. r->parent = parent;
  418. free(r->name);
  419. r->name = estrdup(name);
  420. r->user = f->user;
  421. r->group = f->ram->group;
  422. r->muid = f->ram->muid;
  423. if(prm & DMDIR)
  424. prm = (prm&~0777) | (f->ram->perm&prm&0777);
  425. else
  426. prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
  427. r->perm = prm;
  428. r->ndata = 0;
  429. if(r-ram >= nram)
  430. nram = r - ram + 1;
  431. r->atime = time(0);
  432. r->mtime = r->atime;
  433. f->ram->mtime = r->atime;
  434. f->ram = r;
  435. rhdr.qid = r->qid;
  436. rhdr.iounit = messagesize-IOHDRSZ;
  437. f->open = 1;
  438. if(thdr.mode & ORCLOSE)
  439. f->rclose = 1;
  440. r->open++;
  441. return 0;
  442. }
  443. char*
  444. rread(Fid *f)
  445. {
  446. Ram *r;
  447. uchar *buf;
  448. long off;
  449. int n, m, cnt;
  450. if(f->ram->busy == 0)
  451. return Enotexist;
  452. n = 0;
  453. rhdr.count = 0;
  454. off = thdr.offset;
  455. buf = rdata;
  456. cnt = thdr.count;
  457. if(cnt > messagesize) /* shouldn't happen, anyway */
  458. cnt = messagesize;
  459. if(f->ram->qid.type & QTDIR){
  460. for(r=ram+1; off > 0; r++){
  461. if(r->busy && r->parent==f->ram-ram)
  462. off -= ramstat(r, statbuf, sizeof statbuf);
  463. if(r == &ram[nram-1])
  464. return 0;
  465. }
  466. for(; r<&ram[nram] && n < cnt; r++){
  467. if(!r->busy || r->parent!=f->ram-ram)
  468. continue;
  469. m = ramstat(r, buf+n, cnt-n);
  470. if(m == 0)
  471. break;
  472. n += m;
  473. }
  474. rhdr.data = (char*)rdata;
  475. rhdr.count = n;
  476. return 0;
  477. }
  478. r = f->ram;
  479. if(off >= r->ndata)
  480. return 0;
  481. r->atime = time(0);
  482. n = cnt;
  483. if(off+n > r->ndata)
  484. n = r->ndata - off;
  485. rhdr.data = r->data+off;
  486. rhdr.count = n;
  487. return 0;
  488. }
  489. char*
  490. rwrite(Fid *f)
  491. {
  492. Ram *r;
  493. uint32_t off;
  494. int cnt;
  495. r = f->ram;
  496. if(r->busy == 0)
  497. return Enotexist;
  498. off = thdr.offset;
  499. if(r->perm & DMAPPEND)
  500. off = r->ndata;
  501. cnt = thdr.count;
  502. if(r->qid.type & QTDIR)
  503. return Eisdir;
  504. if(off+cnt >= Maxsize) /* sanity check */
  505. return "write too big";
  506. if(off+cnt > r->ndata)
  507. r->data = erealloc(r->data, off+cnt);
  508. if(off > r->ndata)
  509. memset(r->data+r->ndata, 0, off-r->ndata);
  510. if(off+cnt > r->ndata)
  511. r->ndata = off+cnt;
  512. memmove(r->data+off, thdr.data, cnt);
  513. r->qid.vers++;
  514. r->mtime = time(0);
  515. rhdr.count = cnt;
  516. return 0;
  517. }
  518. void
  519. realremove(Ram *r)
  520. {
  521. r->ndata = 0;
  522. if(r->data)
  523. free(r->data);
  524. r->data = 0;
  525. r->parent = 0;
  526. memset(&r->qid, 0, sizeof r->qid);
  527. free(r->name);
  528. r->name = nil;
  529. r->busy = 0;
  530. }
  531. char *
  532. rclunk(Fid *f)
  533. {
  534. if(f->open)
  535. f->ram->open--;
  536. if(f->rclose)
  537. realremove(f->ram);
  538. f->busy = 0;
  539. f->open = 0;
  540. f->ram = 0;
  541. return 0;
  542. }
  543. char *
  544. rremove(Fid *f)
  545. {
  546. Ram *r;
  547. if(f->open)
  548. f->ram->open--;
  549. f->busy = 0;
  550. f->open = 0;
  551. r = f->ram;
  552. f->ram = 0;
  553. #ifdef CHECKS
  554. if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
  555. return Eperm;
  556. #endif
  557. ram[r->parent].mtime = time(0);
  558. realremove(r);
  559. return 0;
  560. }
  561. char *
  562. rstat(Fid *f)
  563. {
  564. if(f->ram->busy == 0)
  565. return Enotexist;
  566. rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
  567. rhdr.stat = statbuf;
  568. return 0;
  569. }
  570. char *
  571. rwstat(Fid *f)
  572. {
  573. Ram *r, *s;
  574. Dir dir;
  575. if(f->ram->busy == 0)
  576. return Enotexist;
  577. convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
  578. r = f->ram;
  579. /*
  580. * To change length, must have write permission on file.
  581. */
  582. #ifdef CHECKS
  583. if(dir.length!=~0 && dir.length!=r->ndata){
  584. if(!perm(f, r, Pwrite))
  585. return Eperm;
  586. }
  587. #endif
  588. /*
  589. * To change name, must have write permission in parent
  590. * and name must be unique.
  591. */
  592. if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
  593. #ifdef CHECKS
  594. if(!perm(f, &ram[r->parent], Pwrite))
  595. return Eperm;
  596. #endif
  597. for(s=ram; s<&ram[nram]; s++)
  598. if(s->busy && s->parent==r->parent)
  599. if(strcmp(dir.name, s->name)==0)
  600. return Eexist;
  601. }
  602. #ifdef OWNERS
  603. /*
  604. * To change mode, must be owner or group leader.
  605. * Because of lack of users file, leader=>group itself.
  606. */
  607. if(dir.mode!=~0 && r->perm!=dir.mode){
  608. if(strcmp(f->user, r->user) != 0)
  609. if(strcmp(f->user, r->group) != 0)
  610. return Enotowner;
  611. }
  612. /*
  613. * To change group, must be owner and member of new group,
  614. * or leader of current group and leader of new group.
  615. * Second case cannot happen, but we check anyway.
  616. */
  617. if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
  618. if(strcmp(f->user, r->user) == 0)
  619. if(strcmp(f->user, dir.gid) == 0)
  620. goto ok;
  621. if(strcmp(f->user, r->group) == 0)
  622. if(strcmp(f->user, dir.gid) == 0)
  623. goto ok;
  624. return Enotowner;
  625. ok:;
  626. }
  627. #endif
  628. /* all ok; do it */
  629. if(dir.mode != ~0){
  630. dir.mode &= ~DMDIR; /* cannot change dir bit */
  631. dir.mode |= r->perm&DMDIR;
  632. r->perm = dir.mode;
  633. }
  634. if(dir.name[0] != '\0'){
  635. free(r->name);
  636. r->name = estrdup(dir.name);
  637. }
  638. if(dir.gid[0] != '\0')
  639. r->group = atom(dir.gid);
  640. if(dir.uid[0] != '\0')
  641. r->user = atom(dir.uid);
  642. if(dir.length!=~0 && dir.length!=r->ndata){
  643. r->data = erealloc(r->data, dir.length);
  644. if(r->ndata < dir.length)
  645. memset(r->data+r->ndata, 0, dir.length-r->ndata);
  646. r->ndata = dir.length;
  647. }
  648. if(dir.mtime != ~0)
  649. r->mtime = dir.mtime;
  650. ram[r->parent].mtime = time(0);
  651. return 0;
  652. }
  653. uint
  654. ramstat(Ram *r, uchar *buf, uint nbuf)
  655. {
  656. Dir dir;
  657. dir.name = r->name;
  658. dir.qid = r->qid;
  659. dir.mode = r->perm;
  660. dir.length = r->ndata;
  661. dir.uid = r->user;
  662. dir.gid = r->group;
  663. dir.muid = r->muid;
  664. dir.atime = r->atime;
  665. dir.mtime = r->mtime;
  666. return convD2M(&dir, buf, nbuf);
  667. }
  668. Fid *
  669. newfid(int fid)
  670. {
  671. Fid *f, *ff;
  672. ff = 0;
  673. for(f = fids; f; f = f->next)
  674. if(f->fid == fid)
  675. return f;
  676. else if(!ff && !f->busy)
  677. ff = f;
  678. if(ff){
  679. ff->fid = fid;
  680. return ff;
  681. }
  682. f = emalloc(sizeof *f);
  683. f->ram = nil;
  684. f->fid = fid;
  685. f->next = fids;
  686. fids = f;
  687. return f;
  688. }
  689. void
  690. io(void)
  691. {
  692. char *err;
  693. int n, pid;
  694. pid = getpid();
  695. for(;;){
  696. /*
  697. * reading from a pipe or a network device
  698. * will give an error after a few eof reads.
  699. * however, we cannot tell the difference
  700. * between a zero-length read and an interrupt
  701. * on the processes writing to us,
  702. * so we wait for the error.
  703. */
  704. n = read9pmsg(mfd[0], mdata, messagesize);
  705. if(n < 0)
  706. error("mount read: %r");
  707. if(n == 0)
  708. continue;
  709. if(convM2S(mdata, n, &thdr) == 0)
  710. continue;
  711. if(debug)
  712. fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
  713. if(!fcalls[thdr.type])
  714. err = "bad fcall type";
  715. else
  716. err = (*fcalls[thdr.type])(newfid(thdr.fid));
  717. if(err){
  718. rhdr.type = Rerror;
  719. rhdr.ename = err;
  720. }else{
  721. rhdr.type = thdr.type + 1;
  722. rhdr.fid = thdr.fid;
  723. }
  724. rhdr.tag = thdr.tag;
  725. if(debug)
  726. fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
  727. n = convS2M(&rhdr, mdata, messagesize);
  728. if(n == 0)
  729. error("convS2M error on write");
  730. if(write(mfd[1], mdata, n) != n)
  731. error("mount write");
  732. }
  733. }
  734. int
  735. perm(Fid *f, Ram *r, int p)
  736. {
  737. if((p*Pother) & r->perm)
  738. return 1;
  739. if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
  740. return 1;
  741. if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
  742. return 1;
  743. return 0;
  744. }
  745. void *
  746. emalloc(uint32_t n)
  747. {
  748. void *p;
  749. p = malloc(n);
  750. if(!p)
  751. error("out of memory");
  752. memset(p, 0, n);
  753. return p;
  754. }
  755. void *
  756. erealloc(void *p, uint32_t n)
  757. {
  758. p = realloc(p, n);
  759. if(!p)
  760. error("out of memory");
  761. return p;
  762. }
  763. char *
  764. estrdup(char *q)
  765. {
  766. char *p;
  767. int n;
  768. n = strlen(q)+1;
  769. p = malloc(n);
  770. if(!p)
  771. error("out of memory");
  772. memmove(p, q, n);
  773. return p;
  774. }
  775. void
  776. ramfsusage(void)
  777. {
  778. fprint(2, "usage: %s [-is] [-m mountpoint]\n", argv0);
  779. exits("usage");
  780. }
  781. /*
  782. * Custom allocators to avoid malloc overheads on small objects.
  783. * We never free these. (See below.)
  784. */
  785. typedef struct Stringtab Stringtab;
  786. struct Stringtab {
  787. Stringtab *link;
  788. char *str;
  789. };
  790. static Stringtab*
  791. taballoc(void)
  792. {
  793. static Stringtab *t;
  794. static uint nt;
  795. if(nt == 0){
  796. t = malloc(64*sizeof(Stringtab));
  797. if(t == 0)
  798. sysfatal("out of memory");
  799. nt = 64;
  800. }
  801. nt--;
  802. return t++;
  803. }
  804. static char*
  805. xstrdup(char *s)
  806. {
  807. char *r;
  808. int len;
  809. static char *t;
  810. static int nt;
  811. len = strlen(s)+1;
  812. if(len >= 8192)
  813. sysfatal("strdup big string");
  814. if(nt < len){
  815. t = malloc(8192);
  816. if(t == 0)
  817. sysfatal("out of memory");
  818. nt = 8192;
  819. }
  820. r = t;
  821. t += len;
  822. nt -= len;
  823. strcpy(r, s);
  824. return r;
  825. }
  826. /*
  827. * Return a uniquely allocated copy of a string.
  828. * Don't free these -- they stay in the table for the
  829. * next caller who wants that particular string.
  830. * String comparison can be done with pointer comparison
  831. * if you know both strings are atoms.
  832. */
  833. static Stringtab *stab[1024];
  834. static uint
  835. hash(char *s)
  836. {
  837. uint h;
  838. uchar *p;
  839. h = 0;
  840. for(p=(uchar*)s; *p; p++)
  841. h = h*37 + *p;
  842. return h;
  843. }
  844. char*
  845. atom(char *str)
  846. {
  847. uint h;
  848. Stringtab *tab;
  849. h = hash(str) % nelem(stab);
  850. for(tab=stab[h]; tab; tab=tab->link)
  851. if(strcmp(str, tab->str) == 0)
  852. return tab->str;
  853. tab = taballoc();
  854. tab->str = xstrdup(str);
  855. tab->link = stab[h];
  856. stab[h] = tab;
  857. return tab->str;
  858. }