oramfs.c 16 KB

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