ramfs.c 16 KB

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