devmnt.c 21 KB

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