devmnt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. /*
  16. * References are managed as follows:
  17. * The channel to the server - a network connection or pipe - has one
  18. * reference for every Chan open on the server. The server channel has
  19. * c->mux set to the Mnt used for muxing control to that server. Mnts
  20. * have no reference count; they go away when c goes away.
  21. * Each channel derived from the mount point has mchan set to c,
  22. * and increfs/decrefs mchan to manage references on the server
  23. * connection.
  24. */
  25. #define MAXRPC (IOHDRSZ+8192)
  26. struct Mntrpc
  27. {
  28. Chan* c; /* Channel for whom we are working */
  29. Mntrpc* list; /* Free/pending list */
  30. Fcall request; /* Outgoing file system protocol message */
  31. Fcall reply; /* Incoming reply */
  32. Mnt* m; /* Mount device during rpc */
  33. Rendez r; /* Place to hang out */
  34. uint8_t* rpc; /* I/O Data buffer */
  35. uint rpclen; /* len of buffer */
  36. Block *b; /* reply blocks */
  37. char done; /* Rpc completed */
  38. uint64_t stime; /* start time for mnt statistics */
  39. uint32_t reqlen; /* request length for mnt statistics */
  40. uint32_t replen; /* reply length for mnt statistics */
  41. Mntrpc* flushed; /* message this one flushes */
  42. };
  43. enum
  44. {
  45. TAGSHIFT = 5, /* uint32_t has to be 32 bits */
  46. TAGMASK = (1<<TAGSHIFT)-1,
  47. NMASK = (64*1024)>>TAGSHIFT,
  48. };
  49. struct Mntalloc
  50. {
  51. Lock;
  52. Mnt* list; /* Mount devices in use */
  53. Mnt* mntfree; /* Free list */
  54. Mntrpc* rpcfree;
  55. int nrpcfree;
  56. int nrpcused;
  57. uint id;
  58. uint32_t tagmask[NMASK];
  59. }mntalloc;
  60. Mnt* mntchk(Chan*);
  61. void mntdirfix(uint8_t*, Chan*);
  62. Mntrpc* mntflushalloc(Mntrpc*, uint32_t);
  63. void mntflushfree(Mnt*, Mntrpc*);
  64. void mntfree(Mntrpc*);
  65. void mntgate(Mnt*);
  66. void mntpntfree(Mnt*);
  67. void mntqrm(Mnt*, Mntrpc*);
  68. Mntrpc* mntralloc(Chan*, uint32_t);
  69. int32_t mntrdwr(int, Chan*, void*, int32_t, int64_t);
  70. int mntrpcread(Mnt*, Mntrpc*);
  71. void mountio(Mnt*, Mntrpc*);
  72. void mountmux(Mnt*, Mntrpc*);
  73. void mountrpc(Mnt*, Mntrpc*);
  74. int rpcattn(void*);
  75. Chan* mntchan(void);
  76. char Esbadstat[] = "invalid directory entry received from server";
  77. char Enoversion[] = "version not established for mount channel";
  78. void (*mntstats)(int, Chan*, uint64_t, uint32_t);
  79. static void
  80. mntreset(void)
  81. {
  82. mntalloc.id = 1;
  83. mntalloc.tagmask[0] = 1; /* don't allow 0 as a tag */
  84. mntalloc.tagmask[NMASK-1] = 0x80000000UL; /* don't allow NOTAG */
  85. fmtinstall('F', fcallfmt);
  86. fmtinstall('D', dirfmt);
  87. /* We can't install %M since eipfmt does and is used in the kernel [sape] */
  88. if(mfcinit != nil)
  89. mfcinit();
  90. }
  91. /*
  92. * Version is not multiplexed: message sent only once per connection.
  93. */
  94. usize
  95. mntversion(Chan *c, uint32_t msize, char *version, usize returnlen)
  96. {
  97. Mach *m = machp();
  98. Fcall f;
  99. uint8_t *msg;
  100. Mnt *mnt;
  101. char *v;
  102. int32_t l, n;
  103. usize k;
  104. int64_t oo;
  105. char buf[128];
  106. qlock(&c->umqlock); /* make sure no one else does this until we've established ourselves */
  107. if(waserror()){
  108. qunlock(&c->umqlock);
  109. nexterror();
  110. }
  111. /* defaults */
  112. if(msize == 0)
  113. msize = MAXRPC;
  114. if(msize > c->iounit && c->iounit != 0)
  115. msize = c->iounit;
  116. v = version;
  117. if(v == nil || v[0] == '\0')
  118. v = VERSION9P;
  119. /* validity */
  120. if(msize < 0)
  121. error("bad iounit in version call");
  122. if(strncmp(v, VERSION9P, strlen(VERSION9P)) != 0)
  123. error("bad 9P version specification");
  124. mnt = c->mux;
  125. if(mnt != nil){
  126. qunlock(&c->umqlock);
  127. poperror();
  128. strecpy(buf, buf+sizeof buf, mnt->version);
  129. k = strlen(buf);
  130. if(strncmp(buf, v, k) != 0){
  131. snprint(buf, sizeof buf, "incompatible 9P versions %s %s", mnt->version, v);
  132. error(buf);
  133. }
  134. if(returnlen != 0){
  135. if(returnlen < k)
  136. error(Eshort);
  137. memmove(version, buf, k);
  138. }
  139. return k;
  140. }
  141. f.type = Tversion;
  142. f.tag = NOTAG;
  143. f.msize = msize;
  144. f.version = v;
  145. msg = malloc(8192+IOHDRSZ);
  146. if(msg == nil)
  147. exhausted("version memory");
  148. if(waserror()){
  149. free(msg);
  150. nexterror();
  151. }
  152. k = convS2M(&f, msg, 8192+IOHDRSZ);
  153. if(k == 0)
  154. error("bad fversion conversion on send");
  155. lock(c);
  156. oo = c->offset;
  157. c->offset += k;
  158. unlock(c);
  159. l = c->dev->write(c, msg, k, oo);
  160. if(l < k){
  161. lock(c);
  162. c->offset -= k - l;
  163. unlock(c);
  164. error("short write in fversion");
  165. }
  166. /* message sent; receive and decode reply */
  167. n = c->dev->read(c, msg, 8192+IOHDRSZ, c->offset);
  168. if(n <= 0)
  169. error("EOF receiving fversion reply");
  170. lock(c);
  171. c->offset += n;
  172. unlock(c);
  173. l = convM2S(msg, n, &f);
  174. if(l != n)
  175. error("bad fversion conversion on reply");
  176. if(f.type != Rversion){
  177. if(f.type == Rerror)
  178. error(f.ename);
  179. error("unexpected reply type in fversion");
  180. }
  181. if(f.msize > msize)
  182. error("server tries to increase msize in fversion");
  183. if(f.msize<256 || f.msize>1024*1024)
  184. error("nonsense value of msize in fversion");
  185. k = strlen(f.version);
  186. if(strncmp(f.version, v, k) != 0)
  187. error("bad 9P version returned from server");
  188. /* now build Mnt associated with this connection */
  189. lock(&mntalloc);
  190. mnt = mntalloc.mntfree;
  191. if(mnt != nil)
  192. mntalloc.mntfree = mnt->list;
  193. else {
  194. mnt = malloc(sizeof(Mnt));
  195. if(mnt == nil) {
  196. unlock(&mntalloc);
  197. exhausted("mount devices");
  198. }
  199. }
  200. mnt->list = mntalloc.list;
  201. mntalloc.list = mnt;
  202. mnt->version = nil;
  203. kstrdup(&mnt->version, f.version);
  204. mnt->id = mntalloc.id++;
  205. mnt->q = qopen(10*MAXRPC, 0, nil, nil);
  206. mnt->msize = f.msize;
  207. unlock(&mntalloc);
  208. if(returnlen != 0){
  209. if(returnlen < k)
  210. error(Eshort);
  211. memmove(version, f.version, k);
  212. }
  213. poperror(); /* msg */
  214. free(msg);
  215. lock(mnt);
  216. mnt->queue = 0;
  217. mnt->rip = 0;
  218. c->flag |= CMSG;
  219. c->mux = mnt;
  220. mnt->c = c;
  221. unlock(mnt);
  222. poperror(); /* c */
  223. qunlock(&c->umqlock);
  224. return k;
  225. }
  226. Chan*
  227. mntauth(Chan *c, char *spec)
  228. {
  229. Mach *m = machp();
  230. Mnt *mnt;
  231. Mntrpc *r;
  232. mnt = c->mux;
  233. if(mnt == nil){
  234. mntversion(c, MAXRPC, VERSION9P, 0);
  235. mnt = c->mux;
  236. if(mnt == nil)
  237. error(Enoversion);
  238. }
  239. c = mntchan();
  240. if(waserror()) {
  241. /* Close must not be called since it will
  242. * call mnt recursively
  243. */
  244. chanfree(c);
  245. nexterror();
  246. }
  247. r = mntralloc(0, mnt->msize);
  248. if(waserror()) {
  249. mntfree(r);
  250. nexterror();
  251. }
  252. r->request.type = Tauth;
  253. r->request.afid = c->fid;
  254. r->request.uname = m->externup->user;
  255. r->request.aname = spec;
  256. mountrpc(mnt, r);
  257. c->qid = r->reply.aqid;
  258. c->mchan = mnt->c;
  259. incref(mnt->c);
  260. c->mqid = c->qid;
  261. c->mode = ORDWR;
  262. poperror(); /* r */
  263. mntfree(r);
  264. poperror(); /* c */
  265. return c;
  266. }
  267. static Chan*
  268. mntattach(char *muxattach)
  269. {
  270. Mach *m = machp();
  271. Mnt *mnt;
  272. Chan *c;
  273. Mntrpc *r;
  274. struct bogus{
  275. Chan *chan;
  276. Chan *authchan;
  277. char *spec;
  278. int flags;
  279. }bogus;
  280. bogus = *((struct bogus *)muxattach);
  281. c = bogus.chan;
  282. mnt = c->mux;
  283. if(mnt == nil){
  284. mntversion(c, 0, nil, 0);
  285. mnt = c->mux;
  286. if(mnt == nil)
  287. error(Enoversion);
  288. }
  289. c = mntchan();
  290. if(waserror()) {
  291. /* Close must not be called since it will
  292. * call mnt recursively
  293. */
  294. chanfree(c);
  295. nexterror();
  296. }
  297. r = mntralloc(0, mnt->msize);
  298. if(waserror()) {
  299. mntfree(r);
  300. nexterror();
  301. }
  302. r->request.type = Tattach;
  303. r->request.fid = c->fid;
  304. if(bogus.authchan == nil)
  305. r->request.afid = NOFID;
  306. else
  307. r->request.afid = bogus.authchan->fid;
  308. r->request.uname = m->externup->user;
  309. r->request.aname = bogus.spec;
  310. mountrpc(mnt, r);
  311. c->qid = r->reply.qid;
  312. c->mchan = mnt->c;
  313. incref(mnt->c);
  314. c->mqid = c->qid;
  315. poperror(); /* r */
  316. mntfree(r);
  317. poperror(); /* c */
  318. if((bogus.flags & MCACHE) && mfcinit != nil)
  319. c->flag |= CCACHE;
  320. return c;
  321. }
  322. Chan*
  323. mntchan(void)
  324. {
  325. Chan *c;
  326. c = devattach('M', 0);
  327. lock(&mntalloc);
  328. c->devno = mntalloc.id++;
  329. unlock(&mntalloc);
  330. if(c->mchan)
  331. panic("mntchan non-zero %#p", c->mchan);
  332. return c;
  333. }
  334. static Walkqid*
  335. mntwalk(Chan *c, Chan *nc, char **name, int nname)
  336. {
  337. Mach *m = machp();
  338. int i, alloc;
  339. Mnt *mnt;
  340. Mntrpc *r;
  341. Walkqid *wq;
  342. if(nc != nil)
  343. print("mntwalk: nc != nil\n");
  344. if(nname > MAXWELEM)
  345. error("devmnt: too many name elements");
  346. alloc = 0;
  347. wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
  348. if(waserror()){
  349. if(alloc && wq->clone!=nil)
  350. cclose(wq->clone);
  351. free(wq);
  352. return nil;
  353. }
  354. alloc = 0;
  355. mnt = mntchk(c);
  356. r = mntralloc(c, mnt->msize);
  357. if(nc == nil){
  358. nc = devclone(c);
  359. /*
  360. * Until the other side accepts this fid,
  361. * we can't mntclose it.
  362. * nc->dev remains nil for now.
  363. */
  364. alloc = 1;
  365. }
  366. wq->clone = nc;
  367. if(waserror()) {
  368. mntfree(r);
  369. nexterror();
  370. }
  371. r->request.type = Twalk;
  372. r->request.fid = c->fid;
  373. r->request.newfid = nc->fid;
  374. r->request.nwname = nname;
  375. memmove(r->request.wname, name, nname*sizeof(char*));
  376. mountrpc(mnt, r);
  377. if(r->reply.nwqid > nname)
  378. error("too many QIDs returned by walk");
  379. if(r->reply.nwqid < nname){
  380. if(alloc)
  381. cclose(nc);
  382. wq->clone = nil;
  383. if(r->reply.nwqid == 0){
  384. free(wq);
  385. wq = nil;
  386. goto Return;
  387. }
  388. }
  389. /* move new fid onto mnt device and update its qid */
  390. if(wq->clone != nil){
  391. if(wq->clone != c){
  392. wq->clone->dev = c->dev;
  393. //if(wq->clone->dev != nil) //XDYNX
  394. // devtabincr(wq->clone->dev);
  395. wq->clone->mchan = c->mchan;
  396. incref(c->mchan);
  397. }
  398. if(r->reply.nwqid > 0)
  399. wq->clone->qid = r->reply.wqid[r->reply.nwqid-1];
  400. }
  401. wq->nqid = r->reply.nwqid;
  402. for(i=0; i<wq->nqid; i++)
  403. wq->qid[i] = r->reply.wqid[i];
  404. Return:
  405. poperror();
  406. mntfree(r);
  407. poperror();
  408. return wq;
  409. }
  410. static int32_t
  411. mntstat(Chan *c, uint8_t *dp, int32_t n)
  412. {
  413. Mach *m = machp();
  414. Mnt *mnt;
  415. Mntrpc *r;
  416. usize nstat;
  417. if(n < BIT16SZ)
  418. error(Eshortstat);
  419. mnt = mntchk(c);
  420. r = mntralloc(c, mnt->msize);
  421. if(waserror()) {
  422. mntfree(r);
  423. nexterror();
  424. }
  425. r->request.type = Tstat;
  426. r->request.fid = c->fid;
  427. mountrpc(mnt, r);
  428. if(r->reply.nstat > n){
  429. nstat = BIT16SZ;
  430. PBIT16(dp, r->reply.nstat-2);
  431. }else{
  432. nstat = r->reply.nstat;
  433. memmove(dp, r->reply.stat, nstat);
  434. validstat(dp, nstat);
  435. mntdirfix(dp, c);
  436. }
  437. poperror();
  438. mntfree(r);
  439. return nstat;
  440. }
  441. static Chan*
  442. mntopencreate(int type, Chan *c, char *name, int omode, int perm)
  443. {
  444. Mach *m = machp();
  445. Mnt *mnt;
  446. Mntrpc *r;
  447. mnt = mntchk(c);
  448. r = mntralloc(c, mnt->msize);
  449. if(waserror()) {
  450. mntfree(r);
  451. nexterror();
  452. }
  453. r->request.type = type;
  454. r->request.fid = c->fid;
  455. r->request.mode = omode;
  456. if(type == Tcreate){
  457. r->request.perm = perm;
  458. r->request.name = name;
  459. }
  460. mountrpc(mnt, r);
  461. c->qid = r->reply.qid;
  462. c->offset = 0;
  463. c->mode = openmode(omode);
  464. c->iounit = r->reply.iounit;
  465. if(c->iounit == 0 || c->iounit > mnt->msize-IOHDRSZ)
  466. c->iounit = mnt->msize-IOHDRSZ;
  467. c->flag |= COPEN;
  468. poperror();
  469. mntfree(r);
  470. if(c->flag & CCACHE)
  471. mfcopen(c);
  472. return c;
  473. }
  474. static Chan*
  475. mntopen(Chan *c, int omode)
  476. {
  477. return mntopencreate(Topen, c, nil, omode, 0);
  478. }
  479. static void
  480. mntcreate(Chan *c, char *name, int omode, int perm)
  481. {
  482. mntopencreate(Tcreate, c, name, omode, perm);
  483. }
  484. static void
  485. mntclunk(Chan *c, int t)
  486. {
  487. Mach *m = machp();
  488. Mnt *mnt;
  489. Mntrpc *r;
  490. mnt = mntchk(c);
  491. r = mntralloc(c, mnt->msize);
  492. if(waserror()){
  493. mntfree(r);
  494. nexterror();
  495. }
  496. r->request.type = t;
  497. r->request.fid = c->fid;
  498. mountrpc(mnt, r);
  499. mntfree(r);
  500. poperror();
  501. }
  502. void
  503. muxclose(Mnt *mnt)
  504. {
  505. Mntrpc *q, *r;
  506. for(q = mnt->queue; q; q = r) {
  507. r = q->list;
  508. mntfree(q);
  509. }
  510. mnt->id = 0;
  511. free(mnt->version);
  512. mnt->version = nil;
  513. mntpntfree(mnt);
  514. }
  515. void
  516. mntpntfree(Mnt *mnt)
  517. {
  518. Mnt *f, **l;
  519. Queue *q;
  520. lock(&mntalloc);
  521. l = &mntalloc.list;
  522. for(f = *l; f; f = f->list) {
  523. if(f == mnt) {
  524. *l = mnt->list;
  525. break;
  526. }
  527. l = &f->list;
  528. }
  529. mnt->list = mntalloc.mntfree;
  530. mntalloc.mntfree = mnt;
  531. q = mnt->q;
  532. unlock(&mntalloc);
  533. qfree(q);
  534. }
  535. static void
  536. mntclose(Chan *c)
  537. {
  538. mntclunk(c, Tclunk);
  539. }
  540. static void
  541. mntremove(Chan *c)
  542. {
  543. mntclunk(c, Tremove);
  544. }
  545. static int32_t
  546. mntwstat(Chan *c, uint8_t *dp, int32_t n)
  547. {
  548. Mach *m = machp();
  549. Mnt *mnt;
  550. Mntrpc *r;
  551. mnt = mntchk(c);
  552. r = mntralloc(c, mnt->msize);
  553. if(waserror()) {
  554. mntfree(r);
  555. nexterror();
  556. }
  557. r->request.type = Twstat;
  558. r->request.fid = c->fid;
  559. r->request.nstat = n;
  560. r->request.stat = dp;
  561. mountrpc(mnt, r);
  562. poperror();
  563. mntfree(r);
  564. return n;
  565. }
  566. static int32_t
  567. mntread(Chan *c, void *buf, int32_t n, int64_t off)
  568. {
  569. uint8_t *p, *e;
  570. int nc, cache, isdir;
  571. usize dirlen;
  572. isdir = 0;
  573. cache = c->flag & CCACHE;
  574. if(c->qid.type & QTDIR) {
  575. cache = 0;
  576. isdir = 1;
  577. }
  578. p = buf;
  579. if(cache) {
  580. nc = mfcread(c, buf, n, off);
  581. if(nc > 0) {
  582. n -= nc;
  583. if(n == 0)
  584. return nc;
  585. p += nc;
  586. off += nc;
  587. }
  588. n = mntrdwr(Tread, c, p, n, off);
  589. mfcupdate(c, p, n, off);
  590. return n + nc;
  591. }
  592. n = mntrdwr(Tread, c, buf, n, off);
  593. if(isdir) {
  594. for(e = &p[n]; p+BIT16SZ < e; p += dirlen){
  595. dirlen = BIT16SZ+GBIT16(p);
  596. if(p+dirlen > e)
  597. break;
  598. validstat(p, dirlen);
  599. mntdirfix(p, c);
  600. }
  601. if(p != e)
  602. error(Esbadstat);
  603. }
  604. return n;
  605. }
  606. static int32_t
  607. mntwrite(Chan *c, void *buf, int32_t n, int64_t off)
  608. {
  609. return mntrdwr(Twrite, c, buf, n, off);
  610. }
  611. int32_t
  612. mntrdwr(int type, Chan *c, void *buf, int32_t n, int64_t off)
  613. {
  614. Mach *m = machp();
  615. Mnt *mnt;
  616. Mntrpc *r;
  617. char *uba;
  618. int cache;
  619. uint32_t cnt, nr, nreq;
  620. mnt = mntchk(c);
  621. uba = buf;
  622. cnt = 0;
  623. cache = c->flag & CCACHE;
  624. if(c->qid.type & QTDIR)
  625. cache = 0;
  626. for(;;) {
  627. r = mntralloc(c, mnt->msize);
  628. if(waserror()) {
  629. mntfree(r);
  630. nexterror();
  631. }
  632. r->request.type = type;
  633. r->request.fid = c->fid;
  634. r->request.offset = off;
  635. r->request.data = uba;
  636. nr = n;
  637. if(nr > mnt->msize-IOHDRSZ)
  638. nr = mnt->msize-IOHDRSZ;
  639. r->request.count = nr;
  640. mountrpc(mnt, r);
  641. nreq = r->request.count;
  642. nr = r->reply.count;
  643. if(nr > nreq)
  644. nr = nreq;
  645. if(type == Tread)
  646. r->b = bl2mem((uint8_t*)uba, r->b, nr);
  647. else if(cache)
  648. mfcwrite(c, (uint8_t*)uba, nr, off);
  649. poperror();
  650. mntfree(r);
  651. off += nr;
  652. uba += nr;
  653. cnt += nr;
  654. n -= nr;
  655. if(nr != nreq || n == 0 || m->externup->nnote)
  656. break;
  657. }
  658. return cnt;
  659. }
  660. void
  661. mountrpc(Mnt *mnt, Mntrpc *r)
  662. {
  663. Mach *m = machp();
  664. char *sn, *cn;
  665. int t;
  666. r->reply.tag = 0;
  667. r->reply.type = Tmax; /* can't ever be a valid message type */
  668. mountio(mnt, r);
  669. t = r->reply.type;
  670. switch(t) {
  671. case Rerror:
  672. error(r->reply.ename);
  673. case Rflush:
  674. error(Eintr);
  675. default:
  676. if(t == r->request.type+1)
  677. break;
  678. sn = "?";
  679. if(mnt->c->path != nil)
  680. sn = mnt->c->path->s;
  681. cn = "?";
  682. if(r->c != nil && r->c->path != nil)
  683. cn = r->c->path->s;
  684. print("mnt: proc %s %d: mismatch from %s %s rep %#p tag %d fid %d T%d R%d rp %d\n",
  685. m->externup->text, m->externup->pid, sn, cn,
  686. r, r->request.tag, r->request.fid, r->request.type,
  687. r->reply.type, r->reply.tag);
  688. error(Emountrpc);
  689. }
  690. }
  691. void
  692. mountio(Mnt *mnt, Mntrpc *r)
  693. {
  694. Mach *m = machp();
  695. int n;
  696. while(waserror()) {
  697. if(mnt->rip == m->externup)
  698. mntgate(mnt);
  699. if(strcmp(m->externup->errstr, Eintr) != 0){
  700. mntflushfree(mnt, r);
  701. nexterror();
  702. }
  703. r = mntflushalloc(r, mnt->msize);
  704. }
  705. lock(mnt);
  706. r->m = mnt;
  707. r->list = mnt->queue;
  708. mnt->queue = r;
  709. unlock(mnt);
  710. /* Transmit a file system rpc */
  711. if(mnt->msize == 0)
  712. panic("msize");
  713. n = convS2M(&r->request, r->rpc, mnt->msize);
  714. if(n < 0)
  715. panic("bad message type in mountio");
  716. if(mnt->c->dev->write(mnt->c, r->rpc, n, 0) != n)
  717. error(Emountrpc);
  718. r->stime = fastticks(nil);
  719. r->reqlen = n;
  720. /* Gate readers onto the mount point one at a time */
  721. for(;;) {
  722. lock(mnt);
  723. if(mnt->rip == 0)
  724. break;
  725. unlock(mnt);
  726. sleep(&r->r, rpcattn, r);
  727. if(r->done){
  728. poperror();
  729. mntflushfree(mnt, r);
  730. return;
  731. }
  732. }
  733. mnt->rip = m->externup;
  734. unlock(mnt);
  735. while(r->done == 0) {
  736. if(mntrpcread(mnt, r) < 0)
  737. error(Emountrpc);
  738. mountmux(mnt, r);
  739. }
  740. mntgate(mnt);
  741. poperror();
  742. mntflushfree(mnt, r);
  743. }
  744. static int
  745. doread(Mnt *mnt, int len)
  746. {
  747. Block *b;
  748. while(qlen(mnt->q) < len){
  749. b = mnt->c->dev->bread(mnt->c, mnt->msize, 0);
  750. if(b == nil)
  751. return -1;
  752. if(blocklen(b) == 0){
  753. freeblist(b);
  754. return -1;
  755. }
  756. qaddlist(mnt->q, b);
  757. }
  758. return 0;
  759. }
  760. int
  761. mntrpcread(Mnt *mnt, Mntrpc *r)
  762. {
  763. int i, t, len, hlen;
  764. Block *b, **l, *nb;
  765. r->reply.type = 0;
  766. r->reply.tag = 0;
  767. /* read at least length, type, and tag and pullup to a single block */
  768. if(doread(mnt, BIT32SZ+BIT8SZ+BIT16SZ) < 0)
  769. return -1;
  770. nb = pullupqueue(mnt->q, BIT32SZ+BIT8SZ+BIT16SZ);
  771. /* read in the rest of the message, avoid ridiculous (for now) message sizes */
  772. len = GBIT32(nb->rp);
  773. if(len > mnt->msize){
  774. qdiscard(mnt->q, qlen(mnt->q));
  775. return -1;
  776. }
  777. if(doread(mnt, len) < 0)
  778. return -1;
  779. /* pullup the header (i.e. everything except data) */
  780. t = nb->rp[BIT32SZ];
  781. switch(t){
  782. case Rread:
  783. hlen = BIT32SZ+BIT8SZ+BIT16SZ+BIT32SZ;
  784. break;
  785. default:
  786. hlen = len;
  787. break;
  788. }
  789. nb = pullupqueue(mnt->q, hlen);
  790. if(convM2S(nb->rp, len, &r->reply) <= 0){
  791. /* bad message, dump it */
  792. print("mntrpcread: convM2S failed\n");
  793. qdiscard(mnt->q, len);
  794. return -1;
  795. }
  796. /* hang the data off of the fcall struct */
  797. l = &r->b;
  798. *l = nil;
  799. do {
  800. b = qremove(mnt->q);
  801. if(hlen > 0){
  802. b->rp += hlen;
  803. len -= hlen;
  804. hlen = 0;
  805. }
  806. i = BLEN(b);
  807. if(i <= len){
  808. len -= i;
  809. *l = b;
  810. l = &(b->next);
  811. } else {
  812. /* split block and put unused bit back */
  813. nb = allocb(i-len);
  814. memmove(nb->wp, b->rp+len, i-len);
  815. b->wp = b->rp+len;
  816. nb->wp += i-len;
  817. qputback(mnt->q, nb);
  818. *l = b;
  819. return 0;
  820. }
  821. }while(len > 0);
  822. return 0;
  823. }
  824. void
  825. mntgate(Mnt *mnt)
  826. {
  827. Mntrpc *q;
  828. lock(mnt);
  829. mnt->rip = 0;
  830. for(q = mnt->queue; q; q = q->list) {
  831. if(q->done == 0)
  832. if(wakeup(&q->r))
  833. break;
  834. }
  835. unlock(mnt);
  836. }
  837. void
  838. mountmux(Mnt *mnt, Mntrpc *r)
  839. {
  840. Mntrpc **l, *q;
  841. lock(mnt);
  842. l = &mnt->queue;
  843. for(q = *l; q; q = q->list) {
  844. /* look for a reply to a message */
  845. if(q->request.tag == r->reply.tag) {
  846. *l = q->list;
  847. if(q != r) {
  848. /*
  849. * Completed someone else.
  850. * Trade pointers to receive buffer.
  851. */
  852. q->reply = r->reply;
  853. q->b = r->b;
  854. r->b = nil;
  855. }
  856. q->done = 1;
  857. unlock(mnt);
  858. if(mntstats != nil)
  859. (*mntstats)(q->request.type,
  860. mnt->c, q->stime,
  861. q->reqlen + r->replen);
  862. if(q != r)
  863. wakeup(&q->r);
  864. return;
  865. }
  866. l = &q->list;
  867. }
  868. unlock(mnt);
  869. print("unexpected reply tag %ud; type %d\n", r->reply.tag, r->reply.type);
  870. }
  871. /*
  872. * Create a new flush request and chain the previous
  873. * requests from it
  874. */
  875. Mntrpc*
  876. mntflushalloc(Mntrpc *r, uint32_t iounit)
  877. {
  878. Mntrpc *fr;
  879. fr = mntralloc(0, iounit);
  880. fr->request.type = Tflush;
  881. if(r->request.type == Tflush)
  882. fr->request.oldtag = r->request.oldtag;
  883. else
  884. fr->request.oldtag = r->request.tag;
  885. fr->flushed = r;
  886. return fr;
  887. }
  888. /*
  889. * Free a chain of flushes. Remove each unanswered
  890. * flush and the original message from the unanswered
  891. * request queue. Mark the original message as done
  892. * and if it hasn't been answered set the reply to to
  893. * Rflush.
  894. */
  895. void
  896. mntflushfree(Mnt *mnt, Mntrpc *r)
  897. {
  898. Mntrpc *fr;
  899. while(r){
  900. fr = r->flushed;
  901. if(!r->done){
  902. r->reply.type = Rflush;
  903. mntqrm(mnt, r);
  904. }
  905. if(fr)
  906. mntfree(r);
  907. r = fr;
  908. }
  909. }
  910. int
  911. alloctag(void)
  912. {
  913. int i, j;
  914. uint32_t v;
  915. for(i = 0; i < NMASK; i++){
  916. v = mntalloc.tagmask[i];
  917. if(v == ~0UL)
  918. continue;
  919. for(j = 0; j < 1<<TAGSHIFT; j++)
  920. if((v & (1<<j)) == 0){
  921. mntalloc.tagmask[i] |= 1<<j;
  922. return (i<<TAGSHIFT) + j;
  923. }
  924. }
  925. panic("no friggin tags left");
  926. return NOTAG;
  927. }
  928. void
  929. freetag(int t)
  930. {
  931. mntalloc.tagmask[t>>TAGSHIFT] &= ~(1<<(t&TAGMASK));
  932. }
  933. Mntrpc*
  934. mntralloc(Chan *c, uint32_t msize)
  935. {
  936. Mntrpc *new;
  937. lock(&mntalloc);
  938. new = mntalloc.rpcfree;
  939. if(new == nil){
  940. new = malloc(sizeof(Mntrpc));
  941. if(new == nil) {
  942. unlock(&mntalloc);
  943. exhausted("mount rpc header");
  944. }
  945. /*
  946. * The header is split from the data buffer as
  947. * mountmux may swap the buffer with another header.
  948. */
  949. new->rpc = mallocz(msize, 0);
  950. if(new->rpc == nil){
  951. free(new);
  952. unlock(&mntalloc);
  953. exhausted("mount rpc buffer");
  954. }
  955. new->rpclen = msize;
  956. new->request.tag = alloctag();
  957. }
  958. else {
  959. mntalloc.rpcfree = new->list;
  960. mntalloc.nrpcfree--;
  961. if(new->rpclen < msize){
  962. free(new->rpc);
  963. new->rpc = mallocz(msize, 0);
  964. if(new->rpc == nil){
  965. free(new);
  966. mntalloc.nrpcused--;
  967. unlock(&mntalloc);
  968. exhausted("mount rpc buffer");
  969. }
  970. new->rpclen = msize;
  971. }
  972. }
  973. mntalloc.nrpcused++;
  974. unlock(&mntalloc);
  975. new->c = c;
  976. new->done = 0;
  977. new->flushed = nil;
  978. new->b = nil;
  979. return new;
  980. }
  981. void
  982. mntfree(Mntrpc *r)
  983. {
  984. if(r->b != nil)
  985. freeblist(r->b);
  986. lock(&mntalloc);
  987. if(mntalloc.nrpcfree >= 10){
  988. free(r->rpc);
  989. freetag(r->request.tag);
  990. free(r);
  991. }
  992. else{
  993. r->list = mntalloc.rpcfree;
  994. mntalloc.rpcfree = r;
  995. mntalloc.nrpcfree++;
  996. }
  997. mntalloc.nrpcused--;
  998. unlock(&mntalloc);
  999. }
  1000. void
  1001. mntqrm(Mnt *mnt, Mntrpc *r)
  1002. {
  1003. Mntrpc **l, *f;
  1004. lock(mnt);
  1005. r->done = 1;
  1006. l = &mnt->queue;
  1007. for(f = *l; f; f = f->list) {
  1008. if(f == r) {
  1009. *l = r->list;
  1010. break;
  1011. }
  1012. l = &f->list;
  1013. }
  1014. unlock(mnt);
  1015. }
  1016. Mnt*
  1017. mntchk(Chan *c)
  1018. {
  1019. Mnt *mnt;
  1020. /* This routine is mostly vestiges of prior lives; now it's just sanity checking */
  1021. if(c->mchan == nil)
  1022. panic("mntchk 1: nil mchan c %s\n", chanpath(c));
  1023. mnt = c->mchan->mux;
  1024. if(mnt == nil)
  1025. print("mntchk 2: nil mux c %s c->mchan %s \n", chanpath(c), chanpath(c->mchan));
  1026. /*
  1027. * Was it closed and reused (was error(Eshutdown); now, it cannot happen)
  1028. */
  1029. if(mnt->id == 0 || mnt->id >= c->devno)
  1030. panic("mntchk 3: can't happen");
  1031. return mnt;
  1032. }
  1033. /*
  1034. * Rewrite channel type and dev for in-flight data to
  1035. * reflect local values. These entries are known to be
  1036. * the first two in the Dir encoding after the count.
  1037. */
  1038. void
  1039. mntdirfix(uint8_t *dirbuf, Chan *c)
  1040. {
  1041. uint r;
  1042. r = c->dev->dc;
  1043. dirbuf += BIT16SZ; /* skip count */
  1044. PBIT16(dirbuf, r);
  1045. dirbuf += BIT16SZ;
  1046. PBIT32(dirbuf, c->devno);
  1047. }
  1048. int
  1049. rpcattn(void *v)
  1050. {
  1051. Mntrpc *r;
  1052. r = v;
  1053. return r->done || r->m->rip == 0;
  1054. }
  1055. Dev mntdevtab = {
  1056. 'M',
  1057. "mnt",
  1058. mntreset,
  1059. devinit,
  1060. devshutdown,
  1061. mntattach,
  1062. mntwalk,
  1063. mntstat,
  1064. mntopen,
  1065. mntcreate,
  1066. mntclose,
  1067. mntread,
  1068. devbread,
  1069. mntwrite,
  1070. devbwrite,
  1071. mntremove,
  1072. mntwstat,
  1073. };