nfs_socket.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /* $OpenBSD: nfs_socket.c,v 1.110 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $ */
  3. /*
  4. * Copyright (c) 1989, 1991, 1993, 1995
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Rick Macklem at The University of Guelph.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
  35. */
  36. /*
  37. * Socket operations for use by nfs
  38. */
  39. #include <sys/param.h>
  40. #include <sys/systm.h>
  41. #include <sys/proc.h>
  42. #include <sys/mount.h>
  43. #include <sys/kernel.h>
  44. #include <sys/mbuf.h>
  45. #include <sys/vnode.h>
  46. #include <sys/domain.h>
  47. #include <sys/protosw.h>
  48. #include <sys/signalvar.h>
  49. #include <sys/socket.h>
  50. #include <sys/socketvar.h>
  51. #include <sys/syslog.h>
  52. #include <sys/tprintf.h>
  53. #include <sys/namei.h>
  54. #include <sys/pool.h>
  55. #include <sys/queue.h>
  56. #include <netinet/in.h>
  57. #include <netinet/tcp.h>
  58. #include <nfs/rpcv2.h>
  59. #include <nfs/nfsproto.h>
  60. #include <nfs/nfs.h>
  61. #include <nfs/xdr_subs.h>
  62. #include <nfs/nfsm_subs.h>
  63. #include <nfs/nfsmount.h>
  64. #include <nfs/nfs_var.h>
  65. /* External data, mostly RPC constants in XDR form. */
  66. extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
  67. rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr;
  68. extern u_int32_t nfs_prog;
  69. extern struct nfsstats nfsstats;
  70. extern int nfsv3_procid[NFS_NPROCS];
  71. extern int nfs_ticks;
  72. extern struct pool nfsrv_descript_pl;
  73. /*
  74. * There is a congestion window for outstanding rpcs maintained per mount
  75. * point. The cwnd size is adjusted in roughly the way that:
  76. * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
  77. * SIGCOMM '88". ACM, August 1988.
  78. * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
  79. * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
  80. * of rpcs is in progress.
  81. * (The sent count and cwnd are scaled for integer arith.)
  82. * Variants of "slow start" were tried and were found to be too much of a
  83. * performance hit (ave. rtt 3 times larger),
  84. * I suspect due to the large rtt that nfs rpcs have.
  85. */
  86. #define NFS_CWNDSCALE 256
  87. #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
  88. int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256 };
  89. /* RTT estimator */
  90. enum nfs_rto_timers nfs_ptimers[NFS_NPROCS] = {
  91. NFS_DEFAULT_TIMER, /* NULL */
  92. NFS_GETATTR_TIMER, /* GETATTR */
  93. NFS_DEFAULT_TIMER, /* SETATTR */
  94. NFS_LOOKUP_TIMER, /* LOOKUP */
  95. NFS_GETATTR_TIMER, /* ACCESS */
  96. NFS_READ_TIMER, /* READLINK */
  97. NFS_READ_TIMER, /* READ */
  98. NFS_WRITE_TIMER, /* WRITE */
  99. NFS_DEFAULT_TIMER, /* CREATE */
  100. NFS_DEFAULT_TIMER, /* MKDIR */
  101. NFS_DEFAULT_TIMER, /* SYMLINK */
  102. NFS_DEFAULT_TIMER, /* MKNOD */
  103. NFS_DEFAULT_TIMER, /* REMOVE */
  104. NFS_DEFAULT_TIMER, /* RMDIR */
  105. NFS_DEFAULT_TIMER, /* RENAME */
  106. NFS_DEFAULT_TIMER, /* LINK */
  107. NFS_READ_TIMER, /* READDIR */
  108. NFS_READ_TIMER, /* READDIRPLUS */
  109. NFS_DEFAULT_TIMER, /* FSSTAT */
  110. NFS_DEFAULT_TIMER, /* FSINFO */
  111. NFS_DEFAULT_TIMER, /* PATHCONF */
  112. NFS_DEFAULT_TIMER, /* COMMIT */
  113. NFS_DEFAULT_TIMER, /* NOOP */
  114. };
  115. void nfs_init_rtt(struct nfsmount *);
  116. void nfs_update_rtt(struct nfsreq *);
  117. int nfs_estimate_rto(struct nfsmount *, u_int32_t procnum);
  118. void nfs_realign(struct mbuf **, int);
  119. void nfs_realign_fixup(struct mbuf *, struct mbuf *, unsigned int *);
  120. unsigned int nfs_realign_test = 0;
  121. unsigned int nfs_realign_count = 0;
  122. /* Initialize the RTT estimator state for a new mount point. */
  123. void
  124. nfs_init_rtt(struct nfsmount *nmp)
  125. {
  126. int i;
  127. for (i = 0; i < NFS_MAX_TIMER; i++)
  128. nmp->nm_srtt[i] = NFS_INITRTT;
  129. for (i = 0; i < NFS_MAX_TIMER; i++)
  130. nmp->nm_sdrtt[i] = 0;
  131. }
  132. /*
  133. * Update a mount point's RTT estimator state using data from the
  134. * passed-in request.
  135. *
  136. * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
  137. *
  138. * NB: Since the timer resolution of NFS_HZ is so course, it can often
  139. * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
  140. * between N + dt and N + 2 - dt ticks, add 1 before calculating the
  141. * update values.
  142. */
  143. void
  144. nfs_update_rtt(struct nfsreq *rep)
  145. {
  146. int t1 = rep->r_rtt + 1;
  147. int index = nfs_ptimers[rep->r_procnum] - 1;
  148. int *srtt = &rep->r_nmp->nm_srtt[index];
  149. int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
  150. t1 -= *srtt >> 3;
  151. *srtt += t1;
  152. if (t1 < 0)
  153. t1 = -t1;
  154. t1 -= *sdrtt >> 2;
  155. *sdrtt += t1;
  156. }
  157. /*
  158. * Estimate RTO for an NFS RPC sent via an unreliable datagram.
  159. *
  160. * Use the mean and mean deviation of RTT for the appropriate type
  161. * of RPC for the frequent RPCs and a default for the others.
  162. * The justification for doing "other" this way is that these RPCs
  163. * happen so infrequently that timer est. would probably be stale.
  164. * Also, since many of these RPCs are non-idempotent, a conservative
  165. * timeout is desired.
  166. *
  167. * getattr, lookup - A+2D
  168. * read, write - A+4D
  169. * other - nm_timeo
  170. */
  171. int
  172. nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
  173. {
  174. enum nfs_rto_timers timer = nfs_ptimers[procnum];
  175. int index = timer - 1;
  176. int rto;
  177. switch (timer) {
  178. case NFS_GETATTR_TIMER:
  179. case NFS_LOOKUP_TIMER:
  180. rto = ((nmp->nm_srtt[index] + 3) >> 2) +
  181. ((nmp->nm_sdrtt[index] + 1) >> 1);
  182. break;
  183. case NFS_READ_TIMER:
  184. case NFS_WRITE_TIMER:
  185. rto = ((nmp->nm_srtt[index] + 7) >> 3) +
  186. (nmp->nm_sdrtt[index] + 1);
  187. break;
  188. default:
  189. rto = nmp->nm_timeo;
  190. return (rto);
  191. }
  192. if (rto < NFS_MINRTO)
  193. rto = NFS_MINRTO;
  194. else if (rto > NFS_MAXRTO)
  195. rto = NFS_MAXRTO;
  196. return (rto);
  197. }
  198. /*
  199. * Initialize sockets and congestion for a new NFS connection.
  200. * We do not free the sockaddr if error.
  201. */
  202. int
  203. nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
  204. {
  205. struct socket *so;
  206. int s, error, rcvreserve, sndreserve;
  207. struct sockaddr *saddr;
  208. struct sockaddr_in *sin;
  209. struct mbuf *m;
  210. nmp->nm_so = NULL;
  211. saddr = mtod(nmp->nm_nam, struct sockaddr *);
  212. error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
  213. nmp->nm_soproto);
  214. if (error)
  215. goto bad;
  216. so = nmp->nm_so;
  217. nmp->nm_soflags = so->so_proto->pr_flags;
  218. /*
  219. * Some servers require that the client port be a reserved port number.
  220. * We always allocate a reserved port, as this prevents filehandle
  221. * disclosure through UDP port capture.
  222. */
  223. if (saddr->sa_family == AF_INET) {
  224. struct mbuf *mopt;
  225. int *ip;
  226. MGET(mopt, M_WAIT, MT_SOOPTS);
  227. mopt->m_len = sizeof(int);
  228. ip = mtod(mopt, int *);
  229. *ip = IP_PORTRANGE_LOW;
  230. error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
  231. if (error)
  232. goto bad;
  233. MGET(m, M_WAIT, MT_SONAME);
  234. sin = mtod(m, struct sockaddr_in *);
  235. sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
  236. sin->sin_family = AF_INET;
  237. sin->sin_addr.s_addr = INADDR_ANY;
  238. sin->sin_port = htons(0);
  239. error = sobind(so, m, &proc0);
  240. m_freem(m);
  241. if (error)
  242. goto bad;
  243. MGET(mopt, M_WAIT, MT_SOOPTS);
  244. mopt->m_len = sizeof(int);
  245. ip = mtod(mopt, int *);
  246. *ip = IP_PORTRANGE_DEFAULT;
  247. error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
  248. if (error)
  249. goto bad;
  250. }
  251. /*
  252. * Protocols that do not require connections may be optionally left
  253. * unconnected for servers that reply from a port other than NFS_PORT.
  254. */
  255. if (nmp->nm_flag & NFSMNT_NOCONN) {
  256. if (nmp->nm_soflags & PR_CONNREQUIRED) {
  257. error = ENOTCONN;
  258. goto bad;
  259. }
  260. } else {
  261. error = soconnect(so, nmp->nm_nam);
  262. if (error)
  263. goto bad;
  264. /*
  265. * Wait for the connection to complete. Cribbed from the
  266. * connect system call but with the wait timing out so
  267. * that interruptible mounts don't hang here for a long time.
  268. */
  269. s = splsoftnet();
  270. while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
  271. (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
  272. "nfscon", 2 * hz);
  273. if ((so->so_state & SS_ISCONNECTING) &&
  274. so->so_error == 0 && rep &&
  275. (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
  276. so->so_state &= ~SS_ISCONNECTING;
  277. splx(s);
  278. goto bad;
  279. }
  280. }
  281. if (so->so_error) {
  282. error = so->so_error;
  283. so->so_error = 0;
  284. splx(s);
  285. goto bad;
  286. }
  287. splx(s);
  288. }
  289. /*
  290. * Always set receive timeout to detect server crash and reconnect.
  291. * Otherwise, we can get stuck in soreceive forever.
  292. */
  293. so->so_rcv.sb_timeo = (5 * hz);
  294. if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT))
  295. so->so_snd.sb_timeo = (5 * hz);
  296. else
  297. so->so_snd.sb_timeo = 0;
  298. if (nmp->nm_sotype == SOCK_DGRAM) {
  299. sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR;
  300. rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
  301. NFS_MAXPKTHDR) * 2;
  302. } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
  303. sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
  304. rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
  305. NFS_MAXPKTHDR) * 2;
  306. } else {
  307. if (nmp->nm_sotype != SOCK_STREAM)
  308. panic("nfscon sotype");
  309. if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
  310. MGET(m, M_WAIT, MT_SOOPTS);
  311. *mtod(m, int32_t *) = 1;
  312. m->m_len = sizeof(int32_t);
  313. sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
  314. }
  315. if (so->so_proto->pr_protocol == IPPROTO_TCP) {
  316. MGET(m, M_WAIT, MT_SOOPTS);
  317. *mtod(m, int32_t *) = 1;
  318. m->m_len = sizeof(int32_t);
  319. sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
  320. }
  321. sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
  322. sizeof (u_int32_t)) * 2;
  323. rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
  324. sizeof (u_int32_t)) * 2;
  325. }
  326. error = soreserve(so, sndreserve, rcvreserve);
  327. if (error)
  328. goto bad;
  329. so->so_rcv.sb_flags |= SB_NOINTR;
  330. so->so_snd.sb_flags |= SB_NOINTR;
  331. /* Initialize other non-zero congestion variables */
  332. nfs_init_rtt(nmp);
  333. nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
  334. nmp->nm_sent = 0;
  335. nmp->nm_timeouts = 0;
  336. return (0);
  337. bad:
  338. nfs_disconnect(nmp);
  339. return (error);
  340. }
  341. /*
  342. * Reconnect routine:
  343. * Called when a connection is broken on a reliable protocol.
  344. * - clean up the old socket
  345. * - nfs_connect() again
  346. * - set R_MUSTRESEND for all outstanding requests on mount point
  347. * If this fails the mount point is DEAD!
  348. * nb: Must be called with the nfs_sndlock() set on the mount point.
  349. */
  350. int
  351. nfs_reconnect(struct nfsreq *rep)
  352. {
  353. struct nfsreq *rp;
  354. struct nfsmount *nmp = rep->r_nmp;
  355. int s, error;
  356. nfs_disconnect(nmp);
  357. while ((error = nfs_connect(nmp, rep)) != 0) {
  358. if (error == EINTR || error == ERESTART)
  359. return (EINTR);
  360. (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsrecon", 0);
  361. }
  362. /*
  363. * Loop through outstanding request list and fix up all requests
  364. * on old socket.
  365. */
  366. s = splsoftnet();
  367. TAILQ_FOREACH(rp, &nmp->nm_reqsq, r_chain) {
  368. rp->r_flags |= R_MUSTRESEND;
  369. rp->r_rexmit = 0;
  370. }
  371. splx(s);
  372. return (0);
  373. }
  374. /*
  375. * NFS disconnect. Clean up and unlink.
  376. */
  377. void
  378. nfs_disconnect(struct nfsmount *nmp)
  379. {
  380. struct socket *so;
  381. if (nmp->nm_so) {
  382. so = nmp->nm_so;
  383. nmp->nm_so = NULL;
  384. soshutdown(so, SHUT_RDWR);
  385. soclose(so);
  386. }
  387. }
  388. /*
  389. * This is the nfs send routine. For connection based socket types, it
  390. * must be called with an nfs_sndlock() on the socket.
  391. * "rep == NULL" indicates that it has been called from a server.
  392. * For the client side:
  393. * - return EINTR if the RPC is terminated, 0 otherwise
  394. * - set R_MUSTRESEND if the send fails for any reason
  395. * - do any cleanup required by recoverable socket errors (???)
  396. * For the server side:
  397. * - return EINTR or ERESTART if interrupted by a signal
  398. * - return EPIPE if a connection is lost for connection based sockets (TCP...)
  399. * - do any cleanup required by recoverable socket errors (???)
  400. */
  401. int
  402. nfs_send(struct socket *so, struct mbuf *nam, struct mbuf *top,
  403. struct nfsreq *rep)
  404. {
  405. struct mbuf *sendnam;
  406. int error, soflags, flags;
  407. if (rep) {
  408. if (rep->r_flags & R_SOFTTERM) {
  409. m_freem(top);
  410. return (EINTR);
  411. }
  412. if ((so = rep->r_nmp->nm_so) == NULL) {
  413. rep->r_flags |= R_MUSTRESEND;
  414. m_freem(top);
  415. return (0);
  416. }
  417. rep->r_flags &= ~R_MUSTRESEND;
  418. soflags = rep->r_nmp->nm_soflags;
  419. } else
  420. soflags = so->so_proto->pr_flags;
  421. if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
  422. sendnam = NULL;
  423. else
  424. sendnam = nam;
  425. if (so->so_type == SOCK_SEQPACKET)
  426. flags = MSG_EOR;
  427. else
  428. flags = 0;
  429. error = sosend(so, sendnam, NULL, top, NULL, flags);
  430. if (error) {
  431. if (rep) {
  432. /*
  433. * Deal with errors for the client side.
  434. */
  435. if (rep->r_flags & R_SOFTTERM)
  436. error = EINTR;
  437. else
  438. rep->r_flags |= R_MUSTRESEND;
  439. }
  440. /*
  441. * Handle any recoverable (soft) socket errors here. (???)
  442. */
  443. if (error != EINTR && error != ERESTART &&
  444. error != EWOULDBLOCK && error != EPIPE)
  445. error = 0;
  446. }
  447. return (error);
  448. }
  449. #ifdef NFSCLIENT
  450. /*
  451. * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
  452. * done by soreceive(), but for SOCK_STREAM we must deal with the Record
  453. * Mark and consolidate the data into a new mbuf list.
  454. * nb: Sometimes TCP passes the data up to soreceive() in long lists of
  455. * small mbufs.
  456. * For SOCK_STREAM we must be very careful to read an entire record once
  457. * we have read any of it, even if the system call has been interrupted.
  458. */
  459. int
  460. nfs_receive(struct nfsreq *rep, struct mbuf **aname, struct mbuf **mp)
  461. {
  462. struct socket *so;
  463. struct uio auio;
  464. struct iovec aio;
  465. struct mbuf *m;
  466. struct mbuf *control;
  467. u_int32_t len;
  468. struct mbuf **getnam;
  469. int error, sotype, rcvflg;
  470. struct proc *p = curproc; /* XXX */
  471. /*
  472. * Set up arguments for soreceive()
  473. */
  474. *mp = NULL;
  475. *aname = NULL;
  476. sotype = rep->r_nmp->nm_sotype;
  477. /*
  478. * For reliable protocols, lock against other senders/receivers
  479. * in case a reconnect is necessary.
  480. * For SOCK_STREAM, first get the Record Mark to find out how much
  481. * more there is to get.
  482. * We must lock the socket against other receivers
  483. * until we have an entire rpc request/reply.
  484. */
  485. if (sotype != SOCK_DGRAM) {
  486. error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
  487. if (error)
  488. return (error);
  489. tryagain:
  490. /*
  491. * Check for fatal errors and resending request.
  492. */
  493. /*
  494. * Ugh: If a reconnect attempt just happened, nm_so
  495. * would have changed. NULL indicates a failed
  496. * attempt that has essentially shut down this
  497. * mount point.
  498. */
  499. if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
  500. nfs_sndunlock(&rep->r_nmp->nm_flag);
  501. return (EINTR);
  502. }
  503. so = rep->r_nmp->nm_so;
  504. if (!so) {
  505. error = nfs_reconnect(rep);
  506. if (error) {
  507. nfs_sndunlock(&rep->r_nmp->nm_flag);
  508. return (error);
  509. }
  510. goto tryagain;
  511. }
  512. while (rep->r_flags & R_MUSTRESEND) {
  513. m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
  514. nfsstats.rpcretries++;
  515. rep->r_rtt = 0;
  516. rep->r_flags &= ~R_TIMING;
  517. error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
  518. if (error) {
  519. if (error == EINTR || error == ERESTART ||
  520. (error = nfs_reconnect(rep)) != 0) {
  521. nfs_sndunlock(&rep->r_nmp->nm_flag);
  522. return (error);
  523. }
  524. goto tryagain;
  525. }
  526. }
  527. nfs_sndunlock(&rep->r_nmp->nm_flag);
  528. if (sotype == SOCK_STREAM) {
  529. aio.iov_base = (caddr_t) &len;
  530. aio.iov_len = sizeof(u_int32_t);
  531. auio.uio_iov = &aio;
  532. auio.uio_iovcnt = 1;
  533. auio.uio_segflg = UIO_SYSSPACE;
  534. auio.uio_rw = UIO_READ;
  535. auio.uio_offset = 0;
  536. auio.uio_resid = sizeof(u_int32_t);
  537. auio.uio_procp = p;
  538. do {
  539. rcvflg = MSG_WAITALL;
  540. error = soreceive(so, NULL, &auio, NULL, NULL,
  541. &rcvflg, 0);
  542. if (error == EWOULDBLOCK && rep) {
  543. if (rep->r_flags & R_SOFTTERM)
  544. return (EINTR);
  545. /*
  546. * looks like the server died after it
  547. * received the request, make sure
  548. * that we will retransmit and we
  549. * don't get stuck here forever.
  550. */
  551. if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
  552. nfsstats.rpctimeouts++;
  553. error = EPIPE;
  554. }
  555. }
  556. } while (error == EWOULDBLOCK);
  557. if (!error && auio.uio_resid > 0) {
  558. log(LOG_INFO,
  559. "short receive (%zu/%zu) from nfs server %s\n",
  560. sizeof(u_int32_t) - auio.uio_resid,
  561. sizeof(u_int32_t),
  562. rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
  563. error = EPIPE;
  564. }
  565. if (error)
  566. goto errout;
  567. len = ntohl(len) & ~0x80000000;
  568. /*
  569. * This is SERIOUS! We are out of sync with the sender
  570. * and forcing a disconnect/reconnect is all I can do.
  571. */
  572. if (len > NFS_MAXPACKET) {
  573. log(LOG_ERR, "%s (%u) from nfs server %s\n",
  574. "impossible packet length",
  575. len,
  576. rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
  577. error = EFBIG;
  578. goto errout;
  579. }
  580. auio.uio_resid = len;
  581. do {
  582. rcvflg = MSG_WAITALL;
  583. error = soreceive(so, NULL, &auio, mp, NULL,
  584. &rcvflg, 0);
  585. } while (error == EWOULDBLOCK || error == EINTR ||
  586. error == ERESTART);
  587. if (!error && auio.uio_resid > 0) {
  588. log(LOG_INFO,
  589. "short receive (%zu/%u) from nfs server %s\n",
  590. len - auio.uio_resid, len,
  591. rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
  592. error = EPIPE;
  593. }
  594. } else {
  595. /*
  596. * NB: Since uio_resid is big, MSG_WAITALL is ignored
  597. * and soreceive() will return when it has either a
  598. * control msg or a data msg.
  599. * We have no use for control msg., but must grab them
  600. * and then throw them away so we know what is going
  601. * on.
  602. */
  603. auio.uio_resid = len = 100000000; /* Anything Big */
  604. auio.uio_procp = p;
  605. do {
  606. rcvflg = 0;
  607. error = soreceive(so, NULL, &auio, mp, &control,
  608. &rcvflg, 0);
  609. m_freem(control);
  610. if (error == EWOULDBLOCK && rep) {
  611. if (rep->r_flags & R_SOFTTERM)
  612. return (EINTR);
  613. }
  614. } while (error == EWOULDBLOCK ||
  615. (!error && *mp == NULL && control));
  616. if ((rcvflg & MSG_EOR) == 0)
  617. printf("Egad!!\n");
  618. if (!error && *mp == NULL)
  619. error = EPIPE;
  620. len -= auio.uio_resid;
  621. }
  622. errout:
  623. if (error && error != EINTR && error != ERESTART) {
  624. m_freem(*mp);
  625. *mp = NULL;
  626. if (error != EPIPE)
  627. log(LOG_INFO,
  628. "receive error %d from nfs server %s\n",
  629. error,
  630. rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
  631. error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
  632. if (!error) {
  633. error = nfs_reconnect(rep);
  634. if (!error)
  635. goto tryagain;
  636. nfs_sndunlock(&rep->r_nmp->nm_flag);
  637. }
  638. }
  639. } else {
  640. if ((so = rep->r_nmp->nm_so) == NULL)
  641. return (EACCES);
  642. if (so->so_state & SS_ISCONNECTED)
  643. getnam = NULL;
  644. else
  645. getnam = aname;
  646. auio.uio_resid = len = 1000000;
  647. auio.uio_procp = p;
  648. do {
  649. rcvflg = 0;
  650. error = soreceive(so, getnam, &auio, mp, NULL,
  651. &rcvflg, 0);
  652. if (error == EWOULDBLOCK &&
  653. (rep->r_flags & R_SOFTTERM))
  654. return (EINTR);
  655. } while (error == EWOULDBLOCK);
  656. len -= auio.uio_resid;
  657. }
  658. if (error) {
  659. m_freem(*mp);
  660. *mp = NULL;
  661. }
  662. /*
  663. * Search for any mbufs that are not a multiple of 4 bytes long
  664. * or with m_data not longword aligned.
  665. * These could cause pointer alignment problems, so copy them to
  666. * well aligned mbufs.
  667. */
  668. nfs_realign(mp, 5 * NFSX_UNSIGNED);
  669. return (error);
  670. }
  671. /*
  672. * Implement receipt of reply on a socket.
  673. * We must search through the list of received datagrams matching them
  674. * with outstanding requests using the xid, until ours is found.
  675. */
  676. int
  677. nfs_reply(struct nfsreq *myrep)
  678. {
  679. struct nfsreq *rep;
  680. struct nfsmount *nmp = myrep->r_nmp;
  681. struct nfsm_info info;
  682. struct mbuf *nam;
  683. u_int32_t rxid, *tl, t1;
  684. caddr_t cp2;
  685. int s, error;
  686. /*
  687. * Loop around until we get our own reply
  688. */
  689. for (;;) {
  690. /*
  691. * Lock against other receivers so that I don't get stuck in
  692. * sbwait() after someone else has received my reply for me.
  693. * Also necessary for connection based protocols to avoid
  694. * race conditions during a reconnect.
  695. */
  696. error = nfs_rcvlock(myrep);
  697. if (error)
  698. return (error == EALREADY ? 0 : error);
  699. /*
  700. * Get the next Rpc reply off the socket
  701. */
  702. error = nfs_receive(myrep, &nam, &info.nmi_mrep);
  703. nfs_rcvunlock(&nmp->nm_flag);
  704. if (error) {
  705. /*
  706. * Ignore routing errors on connectionless protocols??
  707. */
  708. if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
  709. if (nmp->nm_so)
  710. nmp->nm_so->so_error = 0;
  711. continue;
  712. }
  713. return (error);
  714. }
  715. m_freem(nam);
  716. /*
  717. * Get the xid and check that it is an rpc reply
  718. */
  719. info.nmi_md = info.nmi_mrep;
  720. info.nmi_dpos = mtod(info.nmi_md, caddr_t);
  721. nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
  722. rxid = *tl++;
  723. if (*tl != rpc_reply) {
  724. nfsstats.rpcinvalid++;
  725. m_freem(info.nmi_mrep);
  726. nfsmout:
  727. continue;
  728. }
  729. /*
  730. * Loop through the request list to match up the reply
  731. * Iff no match, just drop the datagram
  732. */
  733. s = splsoftnet();
  734. TAILQ_FOREACH(rep, &nmp->nm_reqsq, r_chain) {
  735. if (rep->r_mrep == NULL && rxid == rep->r_xid) {
  736. /* Found it.. */
  737. rep->r_mrep = info.nmi_mrep;
  738. rep->r_md = info.nmi_md;
  739. rep->r_dpos = info.nmi_dpos;
  740. /*
  741. * Update congestion window.
  742. * Do the additive increase of
  743. * one rpc/rtt.
  744. */
  745. if (nmp->nm_cwnd <= nmp->nm_sent) {
  746. nmp->nm_cwnd +=
  747. (NFS_CWNDSCALE * NFS_CWNDSCALE +
  748. (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
  749. if (nmp->nm_cwnd > NFS_MAXCWND)
  750. nmp->nm_cwnd = NFS_MAXCWND;
  751. }
  752. rep->r_flags &= ~R_SENT;
  753. nmp->nm_sent -= NFS_CWNDSCALE;
  754. if (rep->r_flags & R_TIMING)
  755. nfs_update_rtt(rep);
  756. nmp->nm_timeouts = 0;
  757. break;
  758. }
  759. }
  760. splx(s);
  761. /*
  762. * If not matched to a request, drop it.
  763. * If it's mine, get out.
  764. */
  765. if (rep == 0) {
  766. nfsstats.rpcunexpected++;
  767. m_freem(info.nmi_mrep);
  768. } else if (rep == myrep) {
  769. if (rep->r_mrep == NULL)
  770. panic("nfsreply nil");
  771. return (0);
  772. }
  773. }
  774. }
  775. /*
  776. * nfs_request - goes something like this
  777. * - fill in request struct
  778. * - links it into list
  779. * - calls nfs_send() for first transmit
  780. * - calls nfs_receive() to get reply
  781. * - break down rpc header and return with nfs reply pointed to
  782. * by mrep or error
  783. * nb: always frees up mreq mbuf list
  784. */
  785. int
  786. nfs_request(struct vnode *vp, int procnum, struct nfsm_info *infop)
  787. {
  788. struct mbuf *m;
  789. u_int32_t *tl;
  790. struct nfsmount *nmp;
  791. struct timeval tv;
  792. caddr_t cp2;
  793. int t1, i, s, error = 0;
  794. int trylater_delay;
  795. struct nfsreq *rep;
  796. int mrest_len;
  797. struct nfsm_info info;
  798. rep = pool_get(&nfsreqpl, PR_WAITOK);
  799. rep->r_nmp = VFSTONFS(vp->v_mount);
  800. rep->r_vp = vp;
  801. rep->r_procp = infop->nmi_procp;
  802. rep->r_procnum = procnum;
  803. mrest_len = 0;
  804. m = infop->nmi_mreq;
  805. while (m) {
  806. mrest_len += m->m_len;
  807. m = m->m_next;
  808. }
  809. /* empty mbuf for AUTH_UNIX header */
  810. rep->r_mreq = m_gethdr(M_WAIT, MT_DATA);
  811. rep->r_mreq->m_next = infop->nmi_mreq;
  812. rep->r_mreq->m_pkthdr.len = mrest_len;
  813. trylater_delay = NFS_MINTIMEO;
  814. nmp = rep->r_nmp;
  815. /* Get the RPC header with authorization. */
  816. nfsm_rpchead(rep, infop->nmi_cred, RPCAUTH_UNIX);
  817. m = rep->r_mreq;
  818. /*
  819. * For stream protocols, insert a Sun RPC Record Mark.
  820. */
  821. if (nmp->nm_sotype == SOCK_STREAM) {
  822. M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
  823. *mtod(m, u_int32_t *) = htonl(0x80000000 |
  824. (m->m_pkthdr.len - NFSX_UNSIGNED));
  825. }
  826. tryagain:
  827. rep->r_rtt = rep->r_rexmit = 0;
  828. if (nfs_ptimers[rep->r_procnum] != NFS_DEFAULT_TIMER)
  829. rep->r_flags = R_TIMING;
  830. else
  831. rep->r_flags = 0;
  832. rep->r_mrep = NULL;
  833. /*
  834. * Do the client side RPC.
  835. */
  836. nfsstats.rpcrequests++;
  837. /*
  838. * Chain request into list of outstanding requests. Be sure
  839. * to put it LAST so timer finds oldest requests first.
  840. */
  841. s = splsoftnet();
  842. if (TAILQ_EMPTY(&nmp->nm_reqsq))
  843. timeout_add(&nmp->nm_rtimeout, nfs_ticks);
  844. TAILQ_INSERT_TAIL(&nmp->nm_reqsq, rep, r_chain);
  845. /*
  846. * If backing off another request or avoiding congestion, don't
  847. * send this one now but let timer do it. If not timing a request,
  848. * do it now.
  849. */
  850. if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
  851. (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
  852. nmp->nm_sent < nmp->nm_cwnd)) {
  853. splx(s);
  854. if (nmp->nm_soflags & PR_CONNREQUIRED)
  855. error = nfs_sndlock(&nmp->nm_flag, rep);
  856. if (!error) {
  857. error = nfs_send(nmp->nm_so, nmp->nm_nam,
  858. m_copym(m, 0, M_COPYALL, M_WAIT),
  859. rep);
  860. if (nmp->nm_soflags & PR_CONNREQUIRED)
  861. nfs_sndunlock(&nmp->nm_flag);
  862. }
  863. if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
  864. nmp->nm_sent += NFS_CWNDSCALE;
  865. rep->r_flags |= R_SENT;
  866. }
  867. } else {
  868. splx(s);
  869. rep->r_rtt = -1;
  870. }
  871. /*
  872. * Wait for the reply from our send or the timer's.
  873. */
  874. if (!error || error == EPIPE)
  875. error = nfs_reply(rep);
  876. /*
  877. * RPC done, unlink the request.
  878. */
  879. s = splsoftnet();
  880. TAILQ_REMOVE(&nmp->nm_reqsq, rep, r_chain);
  881. if (TAILQ_EMPTY(&nmp->nm_reqsq))
  882. timeout_del(&nmp->nm_rtimeout);
  883. splx(s);
  884. /*
  885. * Decrement the outstanding request count.
  886. */
  887. if (rep->r_flags & R_SENT) {
  888. rep->r_flags &= ~R_SENT; /* paranoia */
  889. nmp->nm_sent -= NFS_CWNDSCALE;
  890. }
  891. /*
  892. * If there was a successful reply and a tprintf msg.
  893. * tprintf a response.
  894. */
  895. if (!error && (rep->r_flags & R_TPRINTFMSG))
  896. nfs_msg(rep, "is alive again");
  897. info.nmi_mrep = rep->r_mrep;
  898. info.nmi_md = rep->r_md;
  899. info.nmi_dpos = rep->r_dpos;
  900. if (error) {
  901. infop->nmi_mrep = NULL;
  902. goto nfsmout1;
  903. }
  904. /*
  905. * break down the rpc header and check if ok
  906. */
  907. nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  908. if (*tl++ == rpc_msgdenied) {
  909. if (*tl == rpc_mismatch)
  910. error = EOPNOTSUPP;
  911. else
  912. error = EACCES; /* Should be EAUTH. */
  913. infop->nmi_mrep = NULL;
  914. goto nfsmout1;
  915. }
  916. /*
  917. * Since we only support RPCAUTH_UNIX atm we step over the
  918. * reply verifer type, and in the (error) case that there really
  919. * is any data in it, we advance over it.
  920. */
  921. tl++; /* Step over verifer type */
  922. i = fxdr_unsigned(int32_t, *tl);
  923. if (i > 0)
  924. nfsm_adv(nfsm_rndup(i)); /* Should not happen */
  925. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  926. /* 0 == ok */
  927. if (*tl == 0) {
  928. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  929. if (*tl != 0) {
  930. error = fxdr_unsigned(int, *tl);
  931. if ((nmp->nm_flag & NFSMNT_NFSV3) &&
  932. error == NFSERR_TRYLATER) {
  933. m_freem(info.nmi_mrep);
  934. error = 0;
  935. tv.tv_sec = trylater_delay;
  936. tv.tv_usec = 0;
  937. tsleep(&tv, PSOCK, "nfsretry", tvtohz(&tv));
  938. trylater_delay *= NFS_TIMEOUTMUL;
  939. if (trylater_delay > NFS_MAXTIMEO)
  940. trylater_delay = NFS_MAXTIMEO;
  941. goto tryagain;
  942. }
  943. /*
  944. * If the File Handle was stale, invalidate the
  945. * lookup cache, just in case.
  946. */
  947. if (error == ESTALE)
  948. cache_purge(rep->r_vp);
  949. }
  950. goto nfsmout;
  951. }
  952. error = EPROTONOSUPPORT;
  953. nfsmout:
  954. infop->nmi_mrep = info.nmi_mrep;
  955. infop->nmi_md = info.nmi_md;
  956. infop->nmi_dpos = info.nmi_dpos;
  957. nfsmout1:
  958. m_freem(rep->r_mreq);
  959. pool_put(&nfsreqpl, rep);
  960. return (error);
  961. }
  962. #endif /* NFSCLIENT */
  963. /*
  964. * Generate the rpc reply header
  965. * siz arg. is used to decide if adding a cluster is worthwhile
  966. */
  967. int
  968. nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
  969. int err, struct mbuf **mrq, struct mbuf **mbp)
  970. {
  971. u_int32_t *tl;
  972. struct mbuf *mreq;
  973. struct mbuf *mb;
  974. MGETHDR(mreq, M_WAIT, MT_DATA);
  975. mb = mreq;
  976. /*
  977. * If this is a big reply, use a cluster else
  978. * try and leave leading space for the lower level headers.
  979. */
  980. siz += RPC_REPLYSIZ;
  981. if (siz >= MHLEN - max_hdr) {
  982. MCLGET(mreq, M_WAIT);
  983. } else
  984. mreq->m_data += max_hdr;
  985. tl = mtod(mreq, u_int32_t *);
  986. mreq->m_len = 6 * NFSX_UNSIGNED;
  987. *tl++ = txdr_unsigned(nd->nd_retxid);
  988. *tl++ = rpc_reply;
  989. if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
  990. *tl++ = rpc_msgdenied;
  991. if (err & NFSERR_AUTHERR) {
  992. *tl++ = rpc_autherr;
  993. *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
  994. mreq->m_len -= NFSX_UNSIGNED;
  995. } else {
  996. *tl++ = rpc_mismatch;
  997. *tl++ = txdr_unsigned(RPC_VER2);
  998. *tl = txdr_unsigned(RPC_VER2);
  999. }
  1000. } else {
  1001. *tl++ = rpc_msgaccepted;
  1002. /* AUTH_UNIX requires RPCAUTH_NULL. */
  1003. *tl++ = 0;
  1004. *tl++ = 0;
  1005. switch (err) {
  1006. case EPROGUNAVAIL:
  1007. *tl = txdr_unsigned(RPC_PROGUNAVAIL);
  1008. break;
  1009. case EPROGMISMATCH:
  1010. *tl = txdr_unsigned(RPC_PROGMISMATCH);
  1011. tl = nfsm_build(&mb, 2 * NFSX_UNSIGNED);
  1012. *tl++ = txdr_unsigned(NFS_VER2);
  1013. *tl = txdr_unsigned(NFS_VER3);
  1014. break;
  1015. case EPROCUNAVAIL:
  1016. *tl = txdr_unsigned(RPC_PROCUNAVAIL);
  1017. break;
  1018. case EBADRPC:
  1019. *tl = txdr_unsigned(RPC_GARBAGE);
  1020. break;
  1021. default:
  1022. *tl = 0;
  1023. if (err != NFSERR_RETVOID) {
  1024. tl = nfsm_build(&mb, NFSX_UNSIGNED);
  1025. if (err)
  1026. *tl = txdr_unsigned(nfsrv_errmap(nd, err));
  1027. else
  1028. *tl = 0;
  1029. }
  1030. break;
  1031. };
  1032. }
  1033. *mrq = mreq;
  1034. if (mbp != NULL)
  1035. *mbp = mb;
  1036. if (err != 0 && err != NFSERR_RETVOID)
  1037. nfsstats.srvrpc_errs++;
  1038. return (0);
  1039. }
  1040. /*
  1041. * nfs timer routine
  1042. * Scan the nfsreq list and retranmit any requests that have timed out.
  1043. */
  1044. void
  1045. nfs_timer(void *arg)
  1046. {
  1047. struct nfsmount *nmp = arg;
  1048. struct nfsreq *rep;
  1049. struct mbuf *m;
  1050. struct socket *so;
  1051. int timeo, s, error;
  1052. s = splsoftnet();
  1053. TAILQ_FOREACH(rep, &nmp->nm_reqsq, r_chain) {
  1054. if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
  1055. continue;
  1056. if (nfs_sigintr(nmp, rep, rep->r_procp)) {
  1057. rep->r_flags |= R_SOFTTERM;
  1058. continue;
  1059. }
  1060. if (rep->r_rtt >= 0) {
  1061. rep->r_rtt++;
  1062. if (nmp->nm_flag & NFSMNT_DUMBTIMR)
  1063. timeo = nmp->nm_timeo;
  1064. else
  1065. timeo = nfs_estimate_rto(nmp, rep->r_procnum);
  1066. if (nmp->nm_timeouts > 0)
  1067. timeo *= nfs_backoff[nmp->nm_timeouts - 1];
  1068. if (rep->r_rtt <= timeo)
  1069. continue;
  1070. if (nmp->nm_timeouts < nitems(nfs_backoff))
  1071. nmp->nm_timeouts++;
  1072. }
  1073. /* Check for server not responding. */
  1074. if ((rep->r_flags & R_TPRINTFMSG) == 0 && rep->r_rexmit > 4) {
  1075. nfs_msg(rep, "not responding");
  1076. rep->r_flags |= R_TPRINTFMSG;
  1077. }
  1078. if (rep->r_rexmit >= nmp->nm_retry) { /* too many */
  1079. nfsstats.rpctimeouts++;
  1080. rep->r_flags |= R_SOFTTERM;
  1081. continue;
  1082. }
  1083. if (nmp->nm_sotype != SOCK_DGRAM) {
  1084. if (++rep->r_rexmit > NFS_MAXREXMIT)
  1085. rep->r_rexmit = NFS_MAXREXMIT;
  1086. continue;
  1087. }
  1088. if ((so = nmp->nm_so) == NULL)
  1089. continue;
  1090. /*
  1091. * If there is enough space and the window allows..
  1092. * Resend it
  1093. * Set r_rtt to -1 in case we fail to send it now.
  1094. */
  1095. rep->r_rtt = -1;
  1096. if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
  1097. ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
  1098. (rep->r_flags & R_SENT) ||
  1099. nmp->nm_sent < nmp->nm_cwnd) &&
  1100. (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
  1101. if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
  1102. error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
  1103. NULL, NULL, curproc);
  1104. else
  1105. error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
  1106. nmp->nm_nam, NULL, curproc);
  1107. if (error) {
  1108. if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
  1109. so->so_error = 0;
  1110. } else {
  1111. /*
  1112. * Iff first send, start timing
  1113. * else turn timing off, backoff timer
  1114. * and divide congestion window by 2.
  1115. */
  1116. if (rep->r_flags & R_SENT) {
  1117. rep->r_flags &= ~R_TIMING;
  1118. if (++rep->r_rexmit > NFS_MAXREXMIT)
  1119. rep->r_rexmit = NFS_MAXREXMIT;
  1120. nmp->nm_cwnd >>= 1;
  1121. if (nmp->nm_cwnd < NFS_CWNDSCALE)
  1122. nmp->nm_cwnd = NFS_CWNDSCALE;
  1123. nfsstats.rpcretries++;
  1124. } else {
  1125. rep->r_flags |= R_SENT;
  1126. nmp->nm_sent += NFS_CWNDSCALE;
  1127. }
  1128. rep->r_rtt = 0;
  1129. }
  1130. }
  1131. }
  1132. splx(s);
  1133. timeout_add(&nmp->nm_rtimeout, nfs_ticks);
  1134. }
  1135. /*
  1136. * Test for a termination condition pending on the process.
  1137. * This is used for NFSMNT_INT mounts.
  1138. */
  1139. int
  1140. nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct proc *p)
  1141. {
  1142. if (rep && (rep->r_flags & R_SOFTTERM))
  1143. return (EINTR);
  1144. if (!(nmp->nm_flag & NFSMNT_INT))
  1145. return (0);
  1146. if (p && p->p_siglist &&
  1147. (((p->p_siglist & ~p->p_sigmask) &
  1148. ~p->p_p->ps_sigacts->ps_sigignore) & NFSINT_SIGMASK))
  1149. return (EINTR);
  1150. return (0);
  1151. }
  1152. /*
  1153. * Lock a socket against others.
  1154. * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
  1155. * and also to avoid race conditions between the processes with nfs requests
  1156. * in progress when a reconnect is necessary.
  1157. */
  1158. int
  1159. nfs_sndlock(int *flagp, struct nfsreq *rep)
  1160. {
  1161. struct proc *p;
  1162. int slpflag = 0, slptimeo = 0;
  1163. if (rep) {
  1164. p = rep->r_procp;
  1165. if (rep->r_nmp->nm_flag & NFSMNT_INT)
  1166. slpflag = PCATCH;
  1167. } else
  1168. p = NULL;
  1169. while (*flagp & NFSMNT_SNDLOCK) {
  1170. if (rep && nfs_sigintr(rep->r_nmp, rep, p))
  1171. return (EINTR);
  1172. *flagp |= NFSMNT_WANTSND;
  1173. (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
  1174. slptimeo);
  1175. if (slpflag == PCATCH) {
  1176. slpflag = 0;
  1177. slptimeo = 2 * hz;
  1178. }
  1179. }
  1180. *flagp |= NFSMNT_SNDLOCK;
  1181. return (0);
  1182. }
  1183. /*
  1184. * Unlock the stream socket for others.
  1185. */
  1186. void
  1187. nfs_sndunlock(int *flagp)
  1188. {
  1189. if ((*flagp & NFSMNT_SNDLOCK) == 0)
  1190. panic("nfs sndunlock");
  1191. *flagp &= ~NFSMNT_SNDLOCK;
  1192. if (*flagp & NFSMNT_WANTSND) {
  1193. *flagp &= ~NFSMNT_WANTSND;
  1194. wakeup((caddr_t)flagp);
  1195. }
  1196. }
  1197. int
  1198. nfs_rcvlock(struct nfsreq *rep)
  1199. {
  1200. int *flagp = &rep->r_nmp->nm_flag;
  1201. int slpflag, slptimeo = 0;
  1202. if (*flagp & NFSMNT_INT)
  1203. slpflag = PCATCH;
  1204. else
  1205. slpflag = 0;
  1206. while (*flagp & NFSMNT_RCVLOCK) {
  1207. if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
  1208. return (EINTR);
  1209. *flagp |= NFSMNT_WANTRCV;
  1210. (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
  1211. slptimeo);
  1212. if (rep->r_mrep != NULL) {
  1213. /*
  1214. * Don't take the lock if our reply has been received
  1215. * while we where sleeping.
  1216. */
  1217. return (EALREADY);
  1218. }
  1219. if (slpflag == PCATCH) {
  1220. slpflag = 0;
  1221. slptimeo = 2 * hz;
  1222. }
  1223. }
  1224. *flagp |= NFSMNT_RCVLOCK;
  1225. return (0);
  1226. }
  1227. /*
  1228. * Unlock the stream socket for others.
  1229. */
  1230. void
  1231. nfs_rcvunlock(int *flagp)
  1232. {
  1233. if ((*flagp & NFSMNT_RCVLOCK) == 0)
  1234. panic("nfs rcvunlock");
  1235. *flagp &= ~NFSMNT_RCVLOCK;
  1236. if (*flagp & NFSMNT_WANTRCV) {
  1237. *flagp &= ~NFSMNT_WANTRCV;
  1238. wakeup((caddr_t)flagp);
  1239. }
  1240. }
  1241. /*
  1242. * Auxiliary routine to align the length of mbuf copies made with m_copyback().
  1243. */
  1244. void
  1245. nfs_realign_fixup(struct mbuf *m, struct mbuf *n, unsigned int *off)
  1246. {
  1247. size_t padding;
  1248. /*
  1249. * The maximum number of bytes that m_copyback() places in a mbuf is
  1250. * always an aligned quantity, so realign happens at the chain's tail.
  1251. */
  1252. while (n->m_next != NULL)
  1253. n = n->m_next;
  1254. /*
  1255. * Pad from the next elements in the source chain. Loop until the
  1256. * destination chain is aligned, or the end of the source is reached.
  1257. */
  1258. do {
  1259. m = m->m_next;
  1260. if (m == NULL)
  1261. return;
  1262. padding = min(ALIGN(n->m_len) - n->m_len, m->m_len);
  1263. if (padding > M_TRAILINGSPACE(n))
  1264. panic("nfs_realign_fixup: no memory to pad to");
  1265. bcopy(mtod(m, void *), mtod(n, char *) + n->m_len, padding);
  1266. n->m_len += padding;
  1267. m_adj(m, padding);
  1268. *off += padding;
  1269. } while (!ALIGNED_POINTER(n->m_len, void *));
  1270. }
  1271. /*
  1272. * The NFS RPC parsing code uses the data address and the length of mbuf
  1273. * structures to calculate on-memory addresses. This function makes sure these
  1274. * parameters are correctly aligned.
  1275. */
  1276. void
  1277. nfs_realign(struct mbuf **pm, int hsiz)
  1278. {
  1279. struct mbuf *m;
  1280. struct mbuf *n = NULL;
  1281. unsigned int off = 0;
  1282. ++nfs_realign_test;
  1283. while ((m = *pm) != NULL) {
  1284. if (!ALIGNED_POINTER(m->m_data, void *) ||
  1285. !ALIGNED_POINTER(m->m_len, void *)) {
  1286. MGET(n, M_WAIT, MT_DATA);
  1287. #define ALIGN_POINTER(n) ((u_int)(((n) + sizeof(void *)) & ~sizeof(void *)))
  1288. if (ALIGN_POINTER(m->m_len) >= MINCLSIZE) {
  1289. MCLGET(n, M_WAIT);
  1290. }
  1291. n->m_len = 0;
  1292. break;
  1293. }
  1294. pm = &m->m_next;
  1295. }
  1296. /*
  1297. * If n is non-NULL, loop on m copying data, then replace the
  1298. * portion of the chain that had to be realigned.
  1299. */
  1300. if (n != NULL) {
  1301. ++nfs_realign_count;
  1302. while (m) {
  1303. m_copyback(n, off, m->m_len, mtod(m, caddr_t), M_WAIT);
  1304. /*
  1305. * If an unaligned amount of memory was copied, fix up
  1306. * the last mbuf created by m_copyback().
  1307. */
  1308. if (!ALIGNED_POINTER(m->m_len, void *))
  1309. nfs_realign_fixup(m, n, &off);
  1310. off += m->m_len;
  1311. m = m->m_next;
  1312. }
  1313. m_freem(*pm);
  1314. *pm = n;
  1315. }
  1316. }
  1317. /*
  1318. * Parse an RPC request
  1319. * - verify it
  1320. * - fill in the cred struct.
  1321. */
  1322. int
  1323. nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
  1324. {
  1325. int len, i;
  1326. u_int32_t *tl;
  1327. int32_t t1;
  1328. caddr_t cp2;
  1329. u_int32_t nfsvers, auth_type;
  1330. int error = 0;
  1331. struct nfsm_info info;
  1332. info.nmi_mrep = nd->nd_mrep;
  1333. info.nmi_md = nd->nd_md;
  1334. info.nmi_dpos = nd->nd_dpos;
  1335. if (has_header) {
  1336. nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
  1337. nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
  1338. if (*tl++ != rpc_call) {
  1339. m_freem(info.nmi_mrep);
  1340. return (EBADRPC);
  1341. }
  1342. } else
  1343. nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
  1344. nd->nd_repstat = 0;
  1345. nd->nd_flag = 0;
  1346. if (*tl++ != rpc_vers) {
  1347. nd->nd_repstat = ERPCMISMATCH;
  1348. nd->nd_procnum = NFSPROC_NOOP;
  1349. return (0);
  1350. }
  1351. if (*tl != nfs_prog) {
  1352. nd->nd_repstat = EPROGUNAVAIL;
  1353. nd->nd_procnum = NFSPROC_NOOP;
  1354. return (0);
  1355. }
  1356. tl++;
  1357. nfsvers = fxdr_unsigned(u_int32_t, *tl++);
  1358. if (nfsvers != NFS_VER2 && nfsvers != NFS_VER3) {
  1359. nd->nd_repstat = EPROGMISMATCH;
  1360. nd->nd_procnum = NFSPROC_NOOP;
  1361. return (0);
  1362. }
  1363. if (nfsvers == NFS_VER3)
  1364. nd->nd_flag = ND_NFSV3;
  1365. nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
  1366. if (nd->nd_procnum == NFSPROC_NULL)
  1367. return (0);
  1368. if (nd->nd_procnum >= NFS_NPROCS ||
  1369. (nd->nd_procnum > NFSPROC_COMMIT) ||
  1370. (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
  1371. nd->nd_repstat = EPROCUNAVAIL;
  1372. nd->nd_procnum = NFSPROC_NOOP;
  1373. return (0);
  1374. }
  1375. if ((nd->nd_flag & ND_NFSV3) == 0)
  1376. nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
  1377. auth_type = *tl++;
  1378. len = fxdr_unsigned(int, *tl++);
  1379. if (len < 0 || len > RPCAUTH_MAXSIZ) {
  1380. m_freem(info.nmi_mrep);
  1381. return (EBADRPC);
  1382. }
  1383. /* Handle auth_unix */
  1384. if (auth_type == rpc_auth_unix) {
  1385. len = fxdr_unsigned(int, *++tl);
  1386. if (len < 0 || len > NFS_MAXNAMLEN) {
  1387. m_freem(info.nmi_mrep);
  1388. return (EBADRPC);
  1389. }
  1390. nfsm_adv(nfsm_rndup(len));
  1391. nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  1392. memset(&nd->nd_cr, 0, sizeof (struct ucred));
  1393. nd->nd_cr.cr_ref = 1;
  1394. nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
  1395. nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
  1396. len = fxdr_unsigned(int, *tl);
  1397. if (len < 0 || len > RPCAUTH_UNIXGIDS) {
  1398. m_freem(info.nmi_mrep);
  1399. return (EBADRPC);
  1400. }
  1401. nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
  1402. for (i = 0; i < len; i++)
  1403. if (i < NGROUPS_MAX)
  1404. nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
  1405. else
  1406. tl++;
  1407. nd->nd_cr.cr_ngroups = (len > NGROUPS_MAX) ? NGROUPS_MAX : len;
  1408. len = fxdr_unsigned(int, *++tl);
  1409. if (len < 0 || len > RPCAUTH_MAXSIZ) {
  1410. m_freem(info.nmi_mrep);
  1411. return (EBADRPC);
  1412. }
  1413. if (len > 0)
  1414. nfsm_adv(nfsm_rndup(len));
  1415. } else {
  1416. nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
  1417. nd->nd_procnum = NFSPROC_NOOP;
  1418. return (0);
  1419. }
  1420. nd->nd_md = info.nmi_md;
  1421. nd->nd_dpos = info.nmi_dpos;
  1422. return (0);
  1423. nfsmout:
  1424. return (error);
  1425. }
  1426. void
  1427. nfs_msg(struct nfsreq *rep, char *msg)
  1428. {
  1429. tpr_t tpr;
  1430. if (rep->r_procp)
  1431. tpr = tprintf_open(rep->r_procp);
  1432. else
  1433. tpr = NULL;
  1434. tprintf(tpr, "nfs server %s: %s\n",
  1435. rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname, msg);
  1436. tprintf_close(tpr);
  1437. }
  1438. #ifdef NFSSERVER
  1439. /*
  1440. * Socket upcall routine for the nfsd sockets.
  1441. * The caddr_t arg is a pointer to the "struct nfssvc_sock".
  1442. * Essentially do as much as possible non-blocking, else punt and it will
  1443. * be called with M_WAIT from an nfsd.
  1444. */
  1445. void
  1446. nfsrv_rcv(struct socket *so, caddr_t arg, int waitflag)
  1447. {
  1448. struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
  1449. struct mbuf *m;
  1450. struct mbuf *mp, *nam;
  1451. struct uio auio;
  1452. int flags, error;
  1453. if ((slp->ns_flag & SLP_VALID) == 0)
  1454. return;
  1455. #ifdef notdef
  1456. /*
  1457. * Define this to test for nfsds handling this under heavy load.
  1458. */
  1459. if (waitflag == M_DONTWAIT) {
  1460. slp->ns_flag |= SLP_NEEDQ; goto dorecs;
  1461. }
  1462. #endif
  1463. auio.uio_procp = NULL;
  1464. if (so->so_type == SOCK_STREAM) {
  1465. /*
  1466. * If there are already records on the queue, defer soreceive()
  1467. * to an nfsd so that there is feedback to the TCP layer that
  1468. * the nfs servers are heavily loaded.
  1469. */
  1470. if (slp->ns_rec && waitflag == M_DONTWAIT) {
  1471. slp->ns_flag |= SLP_NEEDQ;
  1472. goto dorecs;
  1473. }
  1474. /*
  1475. * Do soreceive().
  1476. */
  1477. auio.uio_resid = 1000000000;
  1478. flags = MSG_DONTWAIT;
  1479. error = soreceive(so, &nam, &auio, &mp, NULL,
  1480. &flags, 0);
  1481. if (error || mp == NULL) {
  1482. if (error == EWOULDBLOCK)
  1483. slp->ns_flag |= SLP_NEEDQ;
  1484. else
  1485. slp->ns_flag |= SLP_DISCONN;
  1486. goto dorecs;
  1487. }
  1488. m = mp;
  1489. if (slp->ns_rawend) {
  1490. slp->ns_rawend->m_next = m;
  1491. slp->ns_cc += 1000000000 - auio.uio_resid;
  1492. } else {
  1493. slp->ns_raw = m;
  1494. slp->ns_cc = 1000000000 - auio.uio_resid;
  1495. }
  1496. while (m->m_next)
  1497. m = m->m_next;
  1498. slp->ns_rawend = m;
  1499. /*
  1500. * Now try and parse record(s) out of the raw stream data.
  1501. */
  1502. error = nfsrv_getstream(slp, waitflag);
  1503. if (error) {
  1504. if (error == EPERM)
  1505. slp->ns_flag |= SLP_DISCONN;
  1506. else
  1507. slp->ns_flag |= SLP_NEEDQ;
  1508. }
  1509. } else {
  1510. do {
  1511. auio.uio_resid = 1000000000;
  1512. flags = MSG_DONTWAIT;
  1513. error = soreceive(so, &nam, &auio, &mp,
  1514. NULL, &flags, 0);
  1515. if (mp) {
  1516. if (nam) {
  1517. m = nam;
  1518. m->m_next = mp;
  1519. } else
  1520. m = mp;
  1521. if (slp->ns_recend)
  1522. slp->ns_recend->m_nextpkt = m;
  1523. else
  1524. slp->ns_rec = m;
  1525. slp->ns_recend = m;
  1526. m->m_nextpkt = NULL;
  1527. }
  1528. if (error) {
  1529. if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
  1530. && error != EWOULDBLOCK) {
  1531. slp->ns_flag |= SLP_DISCONN;
  1532. goto dorecs;
  1533. }
  1534. }
  1535. } while (mp);
  1536. }
  1537. /*
  1538. * Now try and process the request records, non-blocking.
  1539. */
  1540. dorecs:
  1541. if (waitflag == M_DONTWAIT &&
  1542. (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
  1543. nfsrv_wakenfsd(slp);
  1544. }
  1545. /*
  1546. * Try and extract an RPC request from the mbuf data list received on a
  1547. * stream socket. The "waitflag" argument indicates whether or not it
  1548. * can sleep.
  1549. */
  1550. int
  1551. nfsrv_getstream(struct nfssvc_sock *slp, int waitflag)
  1552. {
  1553. struct mbuf *m, **mpp;
  1554. char *cp1, *cp2;
  1555. int len;
  1556. struct mbuf *om, *m2, *recm;
  1557. u_int32_t recmark;
  1558. if (slp->ns_flag & SLP_GETSTREAM)
  1559. return (0);
  1560. slp->ns_flag |= SLP_GETSTREAM;
  1561. for (;;) {
  1562. if (slp->ns_reclen == 0) {
  1563. if (slp->ns_cc < NFSX_UNSIGNED) {
  1564. slp->ns_flag &= ~SLP_GETSTREAM;
  1565. return (0);
  1566. }
  1567. m = slp->ns_raw;
  1568. if (m->m_len >= NFSX_UNSIGNED) {
  1569. bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
  1570. m->m_data += NFSX_UNSIGNED;
  1571. m->m_len -= NFSX_UNSIGNED;
  1572. } else {
  1573. cp1 = (caddr_t)&recmark;
  1574. cp2 = mtod(m, caddr_t);
  1575. while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
  1576. while (m->m_len == 0) {
  1577. m = m->m_next;
  1578. cp2 = mtod(m, caddr_t);
  1579. }
  1580. *cp1++ = *cp2++;
  1581. m->m_data++;
  1582. m->m_len--;
  1583. }
  1584. }
  1585. slp->ns_cc -= NFSX_UNSIGNED;
  1586. recmark = ntohl(recmark);
  1587. slp->ns_reclen = recmark & ~0x80000000;
  1588. if (recmark & 0x80000000)
  1589. slp->ns_flag |= SLP_LASTFRAG;
  1590. else
  1591. slp->ns_flag &= ~SLP_LASTFRAG;
  1592. if (slp->ns_reclen > NFS_MAXPACKET) {
  1593. slp->ns_flag &= ~SLP_GETSTREAM;
  1594. return (EPERM);
  1595. }
  1596. }
  1597. /*
  1598. * Now get the record part.
  1599. */
  1600. recm = NULL;
  1601. if (slp->ns_cc == slp->ns_reclen) {
  1602. recm = slp->ns_raw;
  1603. slp->ns_raw = slp->ns_rawend = NULL;
  1604. slp->ns_cc = slp->ns_reclen = 0;
  1605. } else if (slp->ns_cc > slp->ns_reclen) {
  1606. len = 0;
  1607. m = slp->ns_raw;
  1608. om = NULL;
  1609. while (len < slp->ns_reclen) {
  1610. if ((len + m->m_len) > slp->ns_reclen) {
  1611. m2 = m_copym(m, 0, slp->ns_reclen - len,
  1612. waitflag);
  1613. if (m2) {
  1614. if (om) {
  1615. om->m_next = m2;
  1616. recm = slp->ns_raw;
  1617. } else
  1618. recm = m2;
  1619. m->m_data += slp->ns_reclen - len;
  1620. m->m_len -= slp->ns_reclen - len;
  1621. len = slp->ns_reclen;
  1622. } else {
  1623. slp->ns_flag &= ~SLP_GETSTREAM;
  1624. return (EWOULDBLOCK);
  1625. }
  1626. } else if ((len + m->m_len) == slp->ns_reclen) {
  1627. om = m;
  1628. len += m->m_len;
  1629. m = m->m_next;
  1630. recm = slp->ns_raw;
  1631. om->m_next = NULL;
  1632. } else {
  1633. om = m;
  1634. len += m->m_len;
  1635. m = m->m_next;
  1636. }
  1637. }
  1638. slp->ns_raw = m;
  1639. slp->ns_cc -= len;
  1640. slp->ns_reclen = 0;
  1641. } else {
  1642. slp->ns_flag &= ~SLP_GETSTREAM;
  1643. return (0);
  1644. }
  1645. /*
  1646. * Accumulate the fragments into a record.
  1647. */
  1648. mpp = &slp->ns_frag;
  1649. while (*mpp)
  1650. mpp = &((*mpp)->m_next);
  1651. *mpp = recm;
  1652. if (slp->ns_flag & SLP_LASTFRAG) {
  1653. if (slp->ns_recend)
  1654. slp->ns_recend->m_nextpkt = slp->ns_frag;
  1655. else
  1656. slp->ns_rec = slp->ns_frag;
  1657. slp->ns_recend = slp->ns_frag;
  1658. slp->ns_frag = NULL;
  1659. }
  1660. }
  1661. }
  1662. /*
  1663. * Parse an RPC header.
  1664. */
  1665. int
  1666. nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
  1667. struct nfsrv_descript **ndp)
  1668. {
  1669. struct mbuf *m, *nam;
  1670. struct nfsrv_descript *nd;
  1671. int error;
  1672. *ndp = NULL;
  1673. if ((slp->ns_flag & SLP_VALID) == 0 ||
  1674. (m = slp->ns_rec) == NULL)
  1675. return (ENOBUFS);
  1676. slp->ns_rec = m->m_nextpkt;
  1677. if (slp->ns_rec)
  1678. m->m_nextpkt = NULL;
  1679. else
  1680. slp->ns_recend = NULL;
  1681. if (m->m_type == MT_SONAME) {
  1682. nam = m;
  1683. m = m->m_next;
  1684. nam->m_next = NULL;
  1685. } else
  1686. nam = NULL;
  1687. nd = pool_get(&nfsrv_descript_pl, PR_WAITOK);
  1688. nfs_realign(&m, 10 * NFSX_UNSIGNED);
  1689. nd->nd_md = nd->nd_mrep = m;
  1690. nd->nd_nam2 = nam;
  1691. nd->nd_dpos = mtod(m, caddr_t);
  1692. error = nfs_getreq(nd, nfsd, 1);
  1693. if (error) {
  1694. m_freem(nam);
  1695. pool_put(&nfsrv_descript_pl, nd);
  1696. return (error);
  1697. }
  1698. *ndp = nd;
  1699. nfsd->nfsd_nd = nd;
  1700. return (0);
  1701. }
  1702. /*
  1703. * Search for a sleeping nfsd and wake it up.
  1704. * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
  1705. * running nfsds will go look for the work in the nfssvc_sock list.
  1706. */
  1707. void
  1708. nfsrv_wakenfsd(struct nfssvc_sock *slp)
  1709. {
  1710. struct nfsd *nfsd;
  1711. if ((slp->ns_flag & SLP_VALID) == 0)
  1712. return;
  1713. TAILQ_FOREACH(nfsd, &nfsd_head, nfsd_chain) {
  1714. if (nfsd->nfsd_flag & NFSD_WAITING) {
  1715. nfsd->nfsd_flag &= ~NFSD_WAITING;
  1716. if (nfsd->nfsd_slp)
  1717. panic("nfsd wakeup");
  1718. slp->ns_sref++;
  1719. nfsd->nfsd_slp = slp;
  1720. wakeup_one(nfsd);
  1721. return;
  1722. }
  1723. }
  1724. slp->ns_flag |= SLP_DOREC;
  1725. nfsd_head_flag |= NFSD_CHECKSLP;
  1726. }
  1727. #endif /* NFSSERVER */