solaris10-20040624
[openafs.git] / src / rx / rx_rdwr.c
1  /*
2   * Copyright 2000, International Business Machines Corporation and others.
3   * All Rights Reserved.
4   * 
5   * This software has been released under the terms of the IBM Public
6   * License.  For details, see the LICENSE file in the top-level source
7   * directory or online at http://www.openafs.org/dl/license10.html
8   */
9
10 #include <afsconfig.h>
11 #ifdef KERNEL
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID
18     ("$Header$");
19
20 #ifdef KERNEL
21 #ifndef UKERNEL
22 #ifdef RX_KERNEL_TRACE
23 #include "rx_kcommon.h"
24 #endif
25 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26 #include "afs/sysincludes.h"
27 #else
28 #include "h/types.h"
29 #include "h/time.h"
30 #include "h/stat.h"
31 #ifdef  AFS_OSF_ENV
32 #include <net/net_globals.h>
33 #endif /* AFS_OSF_ENV */
34 #ifdef AFS_LINUX20_ENV
35 #include "h/socket.h"
36 #endif
37 #include "netinet/in.h"
38 #if defined(AFS_SGI_ENV)
39 #include "afs/sysincludes.h"
40 #endif
41 #endif
42 #include "afs/afs_args.h"
43 #include "afs/afs_osi.h"
44 #if     (defined(AFS_AUX_ENV) || defined(AFS_AIX_ENV))
45 #include "h/systm.h"
46 #endif
47 #else /* !UKERNEL */
48 #include "afs/sysincludes.h"
49 #endif /* !UKERNEL */
50 #ifdef RXDEBUG
51 #undef RXDEBUG                  /* turn off debugging */
52 #endif /* RXDEBUG */
53
54 #include "rx_kmutex.h"
55 #include "rx/rx_kernel.h"
56 #include "rx/rx_clock.h"
57 #include "rx/rx_queue.h"
58 #include "rx/rx.h"
59 #include "rx/rx_globals.h"
60 #include "afs/lock.h"
61 #include "afsint.h"
62 #ifdef  AFS_ALPHA_ENV
63 #undef kmem_alloc
64 #undef kmem_free
65 #undef mem_alloc
66 #undef mem_free
67 #undef register
68 #endif /* AFS_ALPHA_ENV */
69 #else /* KERNEL */
70 # include <sys/types.h>
71 #ifndef AFS_NT40_ENV
72 # include <sys/socket.h>
73 # include <sys/file.h>
74 # include <netdb.h>
75 # include <netinet/in.h>
76 # include <sys/stat.h>
77 # include <sys/time.h>
78 #endif
79 #ifdef HAVE_STRING_H
80 #include <string.h>
81 #else
82 #ifdef HAVE_STRINGS_H
83 #include <strings.h>
84 #endif
85 #endif
86 #ifdef HAVE_UNISTD_H
87 #include <unistd.h>
88 #endif
89 # include "rx_user.h"
90 # include "rx_clock.h"
91 # include "rx_queue.h"
92 # include "rx.h"
93 # include "rx_globals.h"
94 #endif /* KERNEL */
95
96 #ifdef RX_LOCKS_DB
97 /* rxdb_fileID is used to identify the lock location, along with line#. */
98 static int rxdb_fileID = RXDB_FILE_RX_RDWR;
99 #endif /* RX_LOCKS_DB */
100 /* rxi_ReadProc -- internal version.
101  *
102  * LOCKS USED -- called at netpri with rx global lock and call->lock held.
103  */
104 int
105 rxi_ReadProc(register struct rx_call *call, register char *buf,
106              register int nbytes)
107 {
108     register struct rx_packet *cp = call->currentPacket;
109     register struct rx_packet *rp;
110     register struct rx_packet *nxp;     /* Next packet pointer, for queue_Scan */
111     register int requestCount;
112     register unsigned int t;
113 /* XXXX took out clock_NewTime from here.  Was it needed? */
114     requestCount = nbytes;
115
116     /* Free any packets from the last call to ReadvProc/WritevProc */
117     if (!queue_IsEmpty(&call->iovq)) {
118         for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
119             queue_Remove(rp);
120             rxi_FreePacket(rp);
121         }
122     }
123
124     do {
125         if (call->nLeft == 0) {
126             /* Get next packet */
127             for (;;) {
128                 if (call->error || (call->mode != RX_MODE_RECEIVING)) {
129                     if (call->error) {
130                         return 0;
131                     }
132                     if (call->mode == RX_MODE_SENDING) {
133                         rxi_FlushWrite(call);
134                         continue;
135                     }
136                 }
137                 if (queue_IsNotEmpty(&call->rq)) {
138                     /* Check that next packet available is next in sequence */
139                     rp = queue_First(&call->rq, rx_packet);
140                     if (rp->header.seq == call->rnext) {
141                         afs_int32 error;
142                         register struct rx_connection *conn = call->conn;
143                         queue_Remove(rp);
144
145                         /* RXS_CheckPacket called to undo RXS_PreparePacket's
146                          * work.  It may reduce the length of the packet by up
147                          * to conn->maxTrailerSize, to reflect the length of the
148                          * data + the header. */
149                         if ((error =
150                              RXS_CheckPacket(conn->securityObject, call,
151                                              rp))) {
152                             /* Used to merely shut down the call, but now we 
153                              * shut down the whole connection since this may 
154                              * indicate an attempt to hijack it */
155
156                             MUTEX_EXIT(&call->lock);
157                             rxi_ConnectionError(conn, error);
158                             MUTEX_ENTER(&conn->conn_data_lock);
159                             rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
160                             MUTEX_EXIT(&conn->conn_data_lock);
161                             rxi_FreePacket(rp);
162                             MUTEX_ENTER(&call->lock);
163
164                             return 0;
165                         }
166                         call->rnext++;
167                         cp = call->currentPacket = rp;
168                         call->curvec = 1;       /* 0th vec is always header */
169                         /* begin at the beginning [ more or less ], continue 
170                          * on until the end, then stop. */
171                         call->curpos =
172                             (char *)cp->wirevec[1].iov_base +
173                             call->conn->securityHeaderSize;
174                         call->curlen =
175                             cp->wirevec[1].iov_len -
176                             call->conn->securityHeaderSize;
177
178                         /* Notice that this code works correctly if the data
179                          * size is 0 (which it may be--no reply arguments from
180                          * server, for example).  This relies heavily on the
181                          * fact that the code below immediately frees the packet
182                          * (no yields, etc.).  If it didn't, this would be a
183                          * problem because a value of zero for call->nLeft
184                          * normally means that there is no read packet */
185                         call->nLeft = cp->length;
186                         hadd32(call->bytesRcvd, cp->length);
187
188                         /* Send a hard ack for every rxi_HardAckRate+1 packets
189                          * consumed. Otherwise schedule an event to send
190                          * the hard ack later on.
191                          */
192                         call->nHardAcks++;
193                         if (!(call->flags & RX_CALL_RECEIVE_DONE)) {
194                             if (call->nHardAcks > (u_short) rxi_HardAckRate) {
195                                 rxevent_Cancel(call->delayedAckEvent, call,
196                                                RX_CALL_REFCOUNT_DELAY);
197                                 rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
198                             } else {
199                                 struct clock when;
200                                 clock_GetTime(&when);
201                                 /* Delay to consolidate ack packets */
202                                 clock_Add(&when, &rx_hardAckDelay);
203                                 if (!call->delayedAckEvent
204                                     || clock_Gt(&call->delayedAckEvent->
205                                                 eventTime, &when)) {
206                                     rxevent_Cancel(call->delayedAckEvent,
207                                                    call,
208                                                    RX_CALL_REFCOUNT_DELAY);
209                                     CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
210                                     call->delayedAckEvent =
211                                         rxevent_Post(&when,
212                                                      rxi_SendDelayedAck, call,
213                                                      0);
214                                 }
215                             }
216                         }
217                         break;
218                     }
219                 }
220
221 /*
222 MTUXXX  doesn't there need to be an "else" here ??? 
223 */
224                 /* Are there ever going to be any more packets? */
225                 if (call->flags & RX_CALL_RECEIVE_DONE) {
226                     return requestCount - nbytes;
227                 }
228                 /* Wait for in-sequence packet */
229                 call->flags |= RX_CALL_READER_WAIT;
230                 clock_NewTime();
231                 call->startWait = clock_Sec();
232                 while (call->flags & RX_CALL_READER_WAIT) {
233 #ifdef  RX_ENABLE_LOCKS
234                     CV_WAIT(&call->cv_rq, &call->lock);
235 #else
236                     osi_rxSleep(&call->rq);
237 #endif
238                 }
239
240                 call->startWait = 0;
241 #ifdef RX_ENABLE_LOCKS
242                 if (call->error) {
243                     return 0;
244                 }
245 #endif /* RX_ENABLE_LOCKS */
246             }
247         } else
248             /* assert(cp); */
249             /* MTUXXX  this should be replaced by some error-recovery code before shipping */
250             /* yes, the following block is allowed to be the ELSE clause (or not) */
251             /* It's possible for call->nLeft to be smaller than any particular
252              * iov_len.  Usually, recvmsg doesn't change the iov_len, since it
253              * reflects the size of the buffer.  We have to keep track of the
254              * number of bytes read in the length field of the packet struct.  On
255              * the final portion of a received packet, it's almost certain that
256              * call->nLeft will be smaller than the final buffer. */
257             while (nbytes && cp) {
258                 t = MIN((int)call->curlen, nbytes);
259                 t = MIN(t, (int)call->nLeft);
260                 memcpy(buf, call->curpos, t);
261                 buf += t;
262                 nbytes -= t;
263                 call->curpos += t;
264                 call->curlen -= t;
265                 call->nLeft -= t;
266
267                 if (!call->nLeft) {
268                     /* out of packet.  Get another one. */
269                     rxi_FreePacket(cp);
270                     cp = call->currentPacket = (struct rx_packet *)0;
271                 } else if (!call->curlen) {
272                     /* need to get another struct iov */
273                     if (++call->curvec >= cp->niovecs) {
274                         /* current packet is exhausted, get ready for another */
275                         /* don't worry about curvec and stuff, they get set somewhere else */
276                         rxi_FreePacket(cp);
277                         cp = call->currentPacket = (struct rx_packet *)0;
278                         call->nLeft = 0;
279                     } else {
280                         call->curpos =
281                             (char *)cp->wirevec[call->curvec].iov_base;
282                         call->curlen = cp->wirevec[call->curvec].iov_len;
283                     }
284                 }
285             }
286         if (!nbytes) {
287             /* user buffer is full, return */
288             return requestCount;
289         }
290
291     } while (nbytes);
292
293     return requestCount;
294 }
295
296 int
297 rx_ReadProc(struct rx_call *call, char *buf, int nbytes)
298 {
299     int bytes;
300     int tcurlen;
301     int tnLeft;
302     char *tcurpos;
303     SPLVAR;
304
305     /*
306      * Free any packets from the last call to ReadvProc/WritevProc.
307      * We do not need the lock because the receiver threads only
308      * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
309      * RX_CALL_IOVEC_WAIT is always cleared before returning from
310      * ReadvProc/WritevProc.
311      */
312     if (!queue_IsEmpty(&call->iovq)) {
313         register struct rx_packet *rp;
314         register struct rx_packet *nxp;
315         for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
316             queue_Remove(rp);
317             rxi_FreePacket(rp);
318         }
319     }
320
321     /*
322      * Most common case, all of the data is in the current iovec.
323      * We do not need the lock because this is the only thread that
324      * updates the curlen, curpos, nLeft fields.
325      *
326      * We are relying on nLeft being zero unless the call is in receive mode.
327      */
328     tcurlen = call->curlen;
329     tnLeft = call->nLeft;
330     if (!call->error && tcurlen > nbytes && tnLeft > nbytes) {
331         tcurpos = call->curpos;
332         memcpy(buf, tcurpos, nbytes);
333         call->curpos = tcurpos + nbytes;
334         call->curlen = tcurlen - nbytes;
335         call->nLeft = tnLeft - nbytes;
336         return nbytes;
337     }
338
339     NETPRI;
340     AFS_RXGLOCK();
341     MUTEX_ENTER(&call->lock);
342     bytes = rxi_ReadProc(call, buf, nbytes);
343     MUTEX_EXIT(&call->lock);
344     AFS_RXGUNLOCK();
345     USERPRI;
346     return bytes;
347 }
348
349 /* Optimization for unmarshalling 32 bit integers */
350 int
351 rx_ReadProc32(struct rx_call *call, afs_int32 * value)
352 {
353     int bytes;
354     int tcurlen;
355     int tnLeft;
356     char *tcurpos;
357     SPLVAR;
358
359     /*
360      * Free any packets from the last call to ReadvProc/WritevProc.
361      * We do not need the lock because the receiver threads only
362      * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
363      * RX_CALL_IOVEC_WAIT is always cleared before returning from
364      * ReadvProc/WritevProc.
365      */
366     if (!queue_IsEmpty(&call->iovq)) {
367         register struct rx_packet *rp;
368         register struct rx_packet *nxp;
369         for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
370             queue_Remove(rp);
371             rxi_FreePacket(rp);
372         }
373     }
374
375     /*
376      * Most common case, all of the data is in the current iovec.
377      * We do not need the lock because this is the only thread that
378      * updates the curlen, curpos, nLeft fields.
379      *
380      * We are relying on nLeft being zero unless the call is in receive mode.
381      */
382     tcurlen = call->curlen;
383     tnLeft = call->nLeft;
384     if (!call->error && tcurlen > sizeof(afs_int32)
385         && tnLeft > sizeof(afs_int32)) {
386         tcurpos = call->curpos;
387         if (!((long)tcurpos & (sizeof(afs_int32) - 1))) {
388             *value = *((afs_int32 *) (tcurpos));
389         } else {
390             memcpy((char *)value, tcurpos, sizeof(afs_int32));
391         }
392         call->curpos = tcurpos + sizeof(afs_int32);
393         call->curlen = tcurlen - sizeof(afs_int32);
394         call->nLeft = tnLeft - sizeof(afs_int32);
395         return sizeof(afs_int32);
396     }
397
398     NETPRI;
399     AFS_RXGLOCK();
400     MUTEX_ENTER(&call->lock);
401     bytes = rxi_ReadProc(call, (char *)value, sizeof(afs_int32));
402     MUTEX_EXIT(&call->lock);
403     AFS_RXGUNLOCK();
404     USERPRI;
405     return bytes;
406 }
407
408 /* rxi_FillReadVec
409  *
410  * Uses packets in the receive queue to fill in as much of the
411  * current iovec as possible. Does not block if it runs out
412  * of packets to complete the iovec. Return true if an ack packet
413  * was sent, otherwise return false */
414 int
415 rxi_FillReadVec(struct rx_call *call, afs_uint32 serial)
416 {
417     int didConsume = 0;
418     int didHardAck = 0;
419     register unsigned int t;
420     struct rx_packet *rp;
421     struct rx_packet *curp;
422     struct iovec *call_iov;
423     struct iovec *cur_iov = NULL;
424
425     curp = call->currentPacket;
426     if (curp) {
427         cur_iov = &curp->wirevec[call->curvec];
428     }
429     call_iov = &call->iov[call->iovNext];
430
431     while (!call->error && call->iovNBytes && call->iovNext < call->iovMax) {
432         if (call->nLeft == 0) {
433             /* Get next packet */
434             if (queue_IsNotEmpty(&call->rq)) {
435                 /* Check that next packet available is next in sequence */
436                 rp = queue_First(&call->rq, rx_packet);
437                 if (rp->header.seq == call->rnext) {
438                     afs_int32 error;
439                     register struct rx_connection *conn = call->conn;
440                     queue_Remove(rp);
441
442                     /* RXS_CheckPacket called to undo RXS_PreparePacket's
443                      * work.  It may reduce the length of the packet by up
444                      * to conn->maxTrailerSize, to reflect the length of the
445                      * data + the header. */
446                     if ((error =
447                          RXS_CheckPacket(conn->securityObject, call, rp))) {
448                         /* Used to merely shut down the call, but now we 
449                          * shut down the whole connection since this may 
450                          * indicate an attempt to hijack it */
451
452                         MUTEX_EXIT(&call->lock);
453                         rxi_ConnectionError(conn, error);
454                         MUTEX_ENTER(&conn->conn_data_lock);
455                         rp = rxi_SendConnectionAbort(conn, rp, 0, 0);
456                         MUTEX_EXIT(&conn->conn_data_lock);
457                         rxi_FreePacket(rp);
458                         MUTEX_ENTER(&call->lock);
459
460                         return 1;
461                     }
462                     call->rnext++;
463                     curp = call->currentPacket = rp;
464                     call->curvec = 1;   /* 0th vec is always header */
465                     cur_iov = &curp->wirevec[1];
466                     /* begin at the beginning [ more or less ], continue 
467                      * on until the end, then stop. */
468                     call->curpos =
469                         (char *)curp->wirevec[1].iov_base +
470                         call->conn->securityHeaderSize;
471                     call->curlen =
472                         curp->wirevec[1].iov_len -
473                         call->conn->securityHeaderSize;
474
475                     /* Notice that this code works correctly if the data
476                      * size is 0 (which it may be--no reply arguments from
477                      * server, for example).  This relies heavily on the
478                      * fact that the code below immediately frees the packet
479                      * (no yields, etc.).  If it didn't, this would be a
480                      * problem because a value of zero for call->nLeft
481                      * normally means that there is no read packet */
482                     call->nLeft = curp->length;
483                     hadd32(call->bytesRcvd, curp->length);
484
485                     /* Send a hard ack for every rxi_HardAckRate+1 packets
486                      * consumed. Otherwise schedule an event to send
487                      * the hard ack later on.
488                      */
489                     call->nHardAcks++;
490                     didConsume = 1;
491                     continue;
492                 }
493             }
494             break;
495         }
496
497         /* It's possible for call->nLeft to be smaller than any particular
498          * iov_len.  Usually, recvmsg doesn't change the iov_len, since it
499          * reflects the size of the buffer.  We have to keep track of the
500          * number of bytes read in the length field of the packet struct.  On
501          * the final portion of a received packet, it's almost certain that
502          * call->nLeft will be smaller than the final buffer. */
503         while (call->iovNBytes && call->iovNext < call->iovMax && curp) {
504
505             t = MIN((int)call->curlen, call->iovNBytes);
506             t = MIN(t, (int)call->nLeft);
507             call_iov->iov_base = call->curpos;
508             call_iov->iov_len = t;
509             call_iov++;
510             call->iovNext++;
511             call->iovNBytes -= t;
512             call->curpos += t;
513             call->curlen -= t;
514             call->nLeft -= t;
515
516             if (!call->nLeft) {
517                 /* out of packet.  Get another one. */
518                 queue_Append(&call->iovq, curp);
519                 curp = call->currentPacket = (struct rx_packet *)0;
520             } else if (!call->curlen) {
521                 /* need to get another struct iov */
522                 if (++call->curvec >= curp->niovecs) {
523                     /* current packet is exhausted, get ready for another */
524                     /* don't worry about curvec and stuff, they get set somewhere else */
525                     queue_Append(&call->iovq, curp);
526                     curp = call->currentPacket = (struct rx_packet *)0;
527                     call->nLeft = 0;
528                 } else {
529                     cur_iov++;
530                     call->curpos = (char *)cur_iov->iov_base;
531                     call->curlen = cur_iov->iov_len;
532                 }
533             }
534         }
535     }
536
537     /* If we consumed any packets then check whether we need to
538      * send a hard ack. */
539     if (didConsume && (!(call->flags & RX_CALL_RECEIVE_DONE))) {
540         if (call->nHardAcks > (u_short) rxi_HardAckRate) {
541             rxevent_Cancel(call->delayedAckEvent, call,
542                            RX_CALL_REFCOUNT_DELAY);
543             rxi_SendAck(call, 0, serial, RX_ACK_DELAY, 0);
544             didHardAck = 1;
545         } else {
546             struct clock when;
547             clock_GetTime(&when);
548             /* Delay to consolidate ack packets */
549             clock_Add(&when, &rx_hardAckDelay);
550             if (!call->delayedAckEvent
551                 || clock_Gt(&call->delayedAckEvent->eventTime, &when)) {
552                 rxevent_Cancel(call->delayedAckEvent, call,
553                                RX_CALL_REFCOUNT_DELAY);
554                 CALL_HOLD(call, RX_CALL_REFCOUNT_DELAY);
555                 call->delayedAckEvent =
556                     rxevent_Post(&when, rxi_SendDelayedAck, call, 0);
557             }
558         }
559     }
560     return didHardAck;
561 }
562
563
564 /* rxi_ReadvProc -- internal version.
565  *
566  * Fills in an iovec with pointers to the packet buffers. All packets
567  * except the last packet (new current packet) are moved to the iovq
568  * while the application is processing the data.
569  *
570  * LOCKS USED -- called at netpri with rx global lock and call->lock held.
571  */
572 int
573 rxi_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
574               int nbytes)
575 {
576     struct rx_packet *rp;
577     struct rx_packet *nxp;      /* Next packet pointer, for queue_Scan */
578     int requestCount;
579     int nextio;
580
581     requestCount = nbytes;
582     nextio = 0;
583
584     /* Free any packets from the last call to ReadvProc/WritevProc */
585     for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
586         queue_Remove(rp);
587         rxi_FreePacket(rp);
588     }
589
590     if (call->mode == RX_MODE_SENDING) {
591         rxi_FlushWrite(call);
592     }
593
594     if (call->error) {
595         return 0;
596     }
597
598     /* Get whatever data is currently available in the receive queue.
599      * If rxi_FillReadVec sends an ack packet then it is possible
600      * that we will receive more data while we drop the call lock
601      * to send the packet. Set the RX_CALL_IOVEC_WAIT flag
602      * here to avoid a race with the receive thread if we send
603      * hard acks in rxi_FillReadVec. */
604     call->flags |= RX_CALL_IOVEC_WAIT;
605     call->iovNBytes = nbytes;
606     call->iovMax = maxio;
607     call->iovNext = 0;
608     call->iov = iov;
609     rxi_FillReadVec(call, 0);
610
611     /* if we need more data then sleep until the receive thread has
612      * filled in the rest. */
613     if (!call->error && call->iovNBytes && call->iovNext < call->iovMax
614         && !(call->flags & RX_CALL_RECEIVE_DONE)) {
615         call->flags |= RX_CALL_READER_WAIT;
616         clock_NewTime();
617         call->startWait = clock_Sec();
618         while (call->flags & RX_CALL_READER_WAIT) {
619 #ifdef  RX_ENABLE_LOCKS
620             CV_WAIT(&call->cv_rq, &call->lock);
621 #else
622             osi_rxSleep(&call->rq);
623 #endif
624         }
625         call->startWait = 0;
626     }
627     call->flags &= ~RX_CALL_IOVEC_WAIT;
628 #ifdef RX_ENABLE_LOCKS
629     if (call->error) {
630         return 0;
631     }
632 #endif /* RX_ENABLE_LOCKS */
633
634     call->iov = NULL;
635     *nio = call->iovNext;
636     return nbytes - call->iovNBytes;
637 }
638
639 int
640 rx_ReadvProc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
641              int nbytes)
642 {
643     int bytes;
644     SPLVAR;
645
646     NETPRI;
647     AFS_RXGLOCK();
648     MUTEX_ENTER(&call->lock);
649     bytes = rxi_ReadvProc(call, iov, nio, maxio, nbytes);
650     MUTEX_EXIT(&call->lock);
651     AFS_RXGUNLOCK();
652     USERPRI;
653     return bytes;
654 }
655
656 /* rxi_WriteProc -- internal version.
657  *
658  * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
659
660 int
661 rxi_WriteProc(register struct rx_call *call, register char *buf,
662               register int nbytes)
663 {
664     struct rx_connection *conn = call->conn;
665     register struct rx_packet *cp = call->currentPacket;
666     register struct rx_packet *tp;      /* Temporary packet pointer */
667     register struct rx_packet *nxp;     /* Next packet pointer, for queue_Scan */
668     register unsigned int t;
669     int requestCount = nbytes;
670
671     /* Free any packets from the last call to ReadvProc/WritevProc */
672     if (!queue_IsEmpty(&call->iovq)) {
673         for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
674             queue_Remove(tp);
675             rxi_FreePacket(tp);
676         }
677     }
678
679     if (call->mode != RX_MODE_SENDING) {
680         if ((conn->type == RX_SERVER_CONNECTION)
681             && (call->mode == RX_MODE_RECEIVING)) {
682             call->mode = RX_MODE_SENDING;
683             if (cp) {
684                 rxi_FreePacket(cp);
685                 cp = call->currentPacket = (struct rx_packet *)0;
686                 call->nLeft = 0;
687                 call->nFree = 0;
688             }
689         } else {
690             return 0;
691         }
692     }
693
694     /* Loop condition is checked at end, so that a write of 0 bytes
695      * will force a packet to be created--specially for the case where
696      * there are 0 bytes on the stream, but we must send a packet
697      * anyway. */
698     do {
699         if (call->nFree == 0) {
700             if (!call->error && cp) {
701 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
702                 /* Wait until TQ_BUSY is reset before adding any
703                  * packets to the transmit queue
704                  */
705                 while (call->flags & RX_CALL_TQ_BUSY) {
706                     call->flags |= RX_CALL_TQ_WAIT;
707 #ifdef RX_ENABLE_LOCKS
708                     CV_WAIT(&call->cv_tq, &call->lock);
709 #else /* RX_ENABLE_LOCKS */
710                     osi_rxSleep(&call->tq);
711 #endif /* RX_ENABLE_LOCKS */
712                 }
713 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
714                 clock_NewTime();        /* Bogus:  need new time package */
715                 /* The 0, below, specifies that it is not the last packet: 
716                  * there will be others. PrepareSendPacket may
717                  * alter the packet length by up to
718                  * conn->securityMaxTrailerSize */
719                 hadd32(call->bytesSent, cp->length);
720                 rxi_PrepareSendPacket(call, cp, 0);
721                 queue_Append(&call->tq, cp);
722                 cp = call->currentPacket = NULL;
723                 if (!
724                     (call->
725                      flags & (RX_CALL_FAST_RECOVER |
726                               RX_CALL_FAST_RECOVER_WAIT))) {
727                     rxi_Start(0, call, 0);
728                 }
729             }
730             /* Wait for transmit window to open up */
731             while (!call->error
732                    && call->tnext + 1 > call->tfirst + call->twind) {
733                 clock_NewTime();
734                 call->startWait = clock_Sec();
735
736 #ifdef  RX_ENABLE_LOCKS
737                 CV_WAIT(&call->cv_twind, &call->lock);
738 #else
739                 call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
740                 osi_rxSleep(&call->twind);
741 #endif
742
743                 call->startWait = 0;
744 #ifdef RX_ENABLE_LOCKS
745                 if (call->error) {
746                     return 0;
747                 }
748 #endif /* RX_ENABLE_LOCKS */
749             }
750             if ((cp = rxi_AllocSendPacket(call, nbytes))) {
751                 call->currentPacket = cp;
752                 call->nFree = cp->length;
753                 call->curvec = 1;       /* 0th vec is always header */
754                 /* begin at the beginning [ more or less ], continue 
755                  * on until the end, then stop. */
756                 call->curpos =
757                     (char *)cp->wirevec[1].iov_base +
758                     call->conn->securityHeaderSize;
759                 call->curlen =
760                     cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
761             }
762             if (call->error) {
763                 if (cp) {
764                     rxi_FreePacket(cp);
765                     call->currentPacket = NULL;
766                 }
767                 return 0;
768             }
769         }
770
771         if (cp && (int)call->nFree < nbytes) {
772             /* Try to extend the current buffer */
773             register int len, mud;
774             len = cp->length;
775             mud = rx_MaxUserDataSize(call);
776             if (mud > len) {
777                 int want;
778                 want = MIN(nbytes - (int)call->nFree, mud - len);
779                 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
780                 if (cp->length > (unsigned)mud)
781                     cp->length = mud;
782                 call->nFree += (cp->length - len);
783             }
784         }
785
786         /* If the remaining bytes fit in the buffer, then store them
787          * and return.  Don't ship a buffer that's full immediately to
788          * the peer--we don't know if it's the last buffer yet */
789
790         if (!cp) {
791             call->nFree = 0;
792         }
793
794         while (nbytes && call->nFree) {
795
796             t = MIN((int)call->curlen, nbytes);
797             t = MIN((int)call->nFree, t);
798             memcpy(call->curpos, buf, t);
799             buf += t;
800             nbytes -= t;
801             call->curpos += t;
802             call->curlen -= t;
803             call->nFree -= t;
804
805             if (!call->curlen) {
806                 /* need to get another struct iov */
807                 if (++call->curvec >= cp->niovecs) {
808                     /* current packet is full, extend or send it */
809                     call->nFree = 0;
810                 } else {
811                     call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
812                     call->curlen = cp->wirevec[call->curvec].iov_len;
813                 }
814             }
815         }                       /* while bytes to send and room to send them */
816
817         /* might be out of space now */
818         if (!nbytes) {
819             return requestCount;
820         } else;                 /* more data to send, so get another packet and keep going */
821     } while (nbytes);
822
823     return requestCount - nbytes;
824 }
825
826 int
827 rx_WriteProc(struct rx_call *call, char *buf, int nbytes)
828 {
829     int bytes;
830     int tcurlen;
831     int tnFree;
832     char *tcurpos;
833     SPLVAR;
834
835     /*
836      * Free any packets from the last call to ReadvProc/WritevProc.
837      * We do not need the lock because the receiver threads only
838      * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
839      * RX_CALL_IOVEC_WAIT is always cleared before returning from
840      * ReadvProc/WritevProc.
841      */
842     if (!queue_IsEmpty(&call->iovq)) {
843         register struct rx_packet *rp;
844         register struct rx_packet *nxp;
845         for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
846             queue_Remove(rp);
847             rxi_FreePacket(rp);
848         }
849     }
850
851     /*
852      * Most common case: all of the data fits in the current iovec.
853      * We do not need the lock because this is the only thread that
854      * updates the curlen, curpos, nFree fields.
855      *
856      * We are relying on nFree being zero unless the call is in send mode.
857      */
858     tcurlen = (int)call->curlen;
859     tnFree = (int)call->nFree;
860     if (!call->error && tcurlen >= nbytes && tnFree >= nbytes) {
861         tcurpos = call->curpos;
862         memcpy(tcurpos, buf, nbytes);
863         call->curpos = tcurpos + nbytes;
864         call->curlen = tcurlen - nbytes;
865         call->nFree = tnFree - nbytes;
866         return nbytes;
867     }
868
869     NETPRI;
870     AFS_RXGLOCK();
871     MUTEX_ENTER(&call->lock);
872     bytes = rxi_WriteProc(call, buf, nbytes);
873     MUTEX_EXIT(&call->lock);
874     AFS_RXGUNLOCK();
875     USERPRI;
876     return bytes;
877 }
878
879 /* Optimization for marshalling 32 bit arguments */
880 int
881 rx_WriteProc32(register struct rx_call *call, register afs_int32 * value)
882 {
883     int bytes;
884     int tcurlen;
885     int tnFree;
886     char *tcurpos;
887     SPLVAR;
888
889     /*
890      * Free any packets from the last call to ReadvProc/WritevProc.
891      * We do not need the lock because the receiver threads only
892      * touch the iovq when the RX_CALL_IOVEC_WAIT flag is set, and the
893      * RX_CALL_IOVEC_WAIT is always cleared before returning from
894      * ReadvProc/WritevProc.
895      */
896     if (!queue_IsEmpty(&call->iovq)) {
897         register struct rx_packet *rp;
898         register struct rx_packet *nxp;
899         for (queue_Scan(&call->iovq, rp, nxp, rx_packet)) {
900             queue_Remove(rp);
901             rxi_FreePacket(rp);
902         }
903     }
904
905     /*
906      * Most common case: all of the data fits in the current iovec.
907      * We do not need the lock because this is the only thread that
908      * updates the curlen, curpos, nFree fields.
909      *
910      * We are relying on nFree being zero unless the call is in send mode.
911      */
912     tcurlen = (int)call->curlen;
913     tnFree = (int)call->nFree;
914     if (!call->error && tcurlen >= sizeof(afs_int32)
915         && tnFree >= sizeof(afs_int32)) {
916         tcurpos = call->curpos;
917         if (!((long)tcurpos & (sizeof(afs_int32) - 1))) {
918             *((afs_int32 *) (tcurpos)) = *value;
919         } else {
920             memcpy(tcurpos, (char *)value, sizeof(afs_int32));
921         }
922         call->curpos = tcurpos + sizeof(afs_int32);
923         call->curlen = tcurlen - sizeof(afs_int32);
924         call->nFree = tnFree - sizeof(afs_int32);
925         return sizeof(afs_int32);
926     }
927
928     NETPRI;
929     AFS_RXGLOCK();
930     MUTEX_ENTER(&call->lock);
931     bytes = rxi_WriteProc(call, (char *)value, sizeof(afs_int32));
932     MUTEX_EXIT(&call->lock);
933     AFS_RXGUNLOCK();
934     USERPRI;
935     return bytes;
936 }
937
938 /* rxi_WritevAlloc -- internal version.
939  *
940  * Fill in an iovec to point to data in packet buffers. The application
941  * calls rxi_WritevProc when the buffers are full.
942  *
943  * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
944
945 int
946 rxi_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
947                 int nbytes)
948 {
949     struct rx_connection *conn = call->conn;
950     struct rx_packet *cp = call->currentPacket;
951     struct rx_packet *tp;       /* temporary packet pointer */
952     struct rx_packet *nxp;      /* Next packet pointer, for queue_Scan */
953     int requestCount;
954     int nextio;
955     /* Temporary values, real work is done in rxi_WritevProc */
956     int tnFree;
957     int tcurvec;
958     char *tcurpos;
959     int tcurlen;
960
961     requestCount = nbytes;
962     nextio = 0;
963
964     /* Free any packets from the last call to ReadvProc/WritevProc */
965     for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
966         queue_Remove(tp);
967         rxi_FreePacket(tp);
968     }
969
970     if (call->mode != RX_MODE_SENDING) {
971         if ((conn->type == RX_SERVER_CONNECTION)
972             && (call->mode == RX_MODE_RECEIVING)) {
973             call->mode = RX_MODE_SENDING;
974             if (cp) {
975                 rxi_FreePacket(cp);
976                 cp = call->currentPacket = (struct rx_packet *)0;
977                 call->nLeft = 0;
978                 call->nFree = 0;
979             }
980         } else {
981             return 0;
982         }
983     }
984
985     /* Set up the iovec to point to data in packet buffers. */
986     tnFree = call->nFree;
987     tcurvec = call->curvec;
988     tcurpos = call->curpos;
989     tcurlen = call->curlen;
990     do {
991         register unsigned int t;
992
993         if (tnFree == 0) {
994             /* current packet is full, allocate a new one */
995             cp = rxi_AllocSendPacket(call, nbytes);
996             if (cp == NULL) {
997                 /* out of space, return what we have */
998                 *nio = nextio;
999                 return requestCount - nbytes;
1000             }
1001             queue_Append(&call->iovq, cp);
1002             tnFree = cp->length;
1003             tcurvec = 1;
1004             tcurpos =
1005                 (char *)cp->wirevec[1].iov_base +
1006                 call->conn->securityHeaderSize;
1007             tcurlen = cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1008         }
1009
1010         if (tnFree < nbytes) {
1011             /* try to extend the current packet */
1012             register int len, mud;
1013             len = cp->length;
1014             mud = rx_MaxUserDataSize(call);
1015             if (mud > len) {
1016                 int want;
1017                 want = MIN(nbytes - tnFree, mud - len);
1018                 rxi_AllocDataBuf(cp, want, RX_PACKET_CLASS_SEND_CBUF);
1019                 if (cp->length > (unsigned)mud)
1020                     cp->length = mud;
1021                 tnFree += (cp->length - len);
1022                 if (cp == call->currentPacket) {
1023                     call->nFree += (cp->length - len);
1024                 }
1025             }
1026         }
1027
1028         /* fill in the next entry in the iovec */
1029         t = MIN(tcurlen, nbytes);
1030         t = MIN(tnFree, t);
1031         iov[nextio].iov_base = tcurpos;
1032         iov[nextio].iov_len = t;
1033         nbytes -= t;
1034         tcurpos += t;
1035         tcurlen -= t;
1036         tnFree -= t;
1037         nextio++;
1038
1039         if (!tcurlen) {
1040             /* need to get another struct iov */
1041             if (++tcurvec >= cp->niovecs) {
1042                 /* current packet is full, extend it or move on to next packet */
1043                 tnFree = 0;
1044             } else {
1045                 tcurpos = (char *)cp->wirevec[tcurvec].iov_base;
1046                 tcurlen = cp->wirevec[tcurvec].iov_len;
1047             }
1048         }
1049     } while (nbytes && nextio < maxio);
1050     *nio = nextio;
1051     return requestCount - nbytes;
1052 }
1053
1054 int
1055 rx_WritevAlloc(struct rx_call *call, struct iovec *iov, int *nio, int maxio,
1056                int nbytes)
1057 {
1058     int bytes;
1059     SPLVAR;
1060
1061     NETPRI;
1062     AFS_RXGLOCK();
1063     MUTEX_ENTER(&call->lock);
1064     bytes = rxi_WritevAlloc(call, iov, nio, maxio, nbytes);
1065     MUTEX_EXIT(&call->lock);
1066     AFS_RXGUNLOCK();
1067     USERPRI;
1068     return bytes;
1069 }
1070
1071 /* rxi_WritevProc -- internal version.
1072  *
1073  * Send buffers allocated in rxi_WritevAlloc.
1074  *
1075  * LOCKS USED -- called at netpri with rx global lock and call->lock held. */
1076
1077 int
1078 rxi_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1079 {
1080     struct rx_packet *cp = call->currentPacket;
1081     register struct rx_packet *tp;      /* Temporary packet pointer */
1082     register struct rx_packet *nxp;     /* Next packet pointer, for queue_Scan */
1083     int nextio;
1084     int requestCount;
1085     struct rx_queue tmpq;
1086
1087     requestCount = nbytes;
1088     nextio = 0;
1089
1090     if (call->mode != RX_MODE_SENDING) {
1091         call->error = RX_PROTOCOL_ERROR;
1092     }
1093 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1094     /* Wait until TQ_BUSY is reset before trying to move any
1095      * packets to the transmit queue.  */
1096     while (!call->error && call->flags & RX_CALL_TQ_BUSY) {
1097         call->flags |= RX_CALL_TQ_WAIT;
1098 #ifdef RX_ENABLE_LOCKS
1099         CV_WAIT(&call->cv_tq, &call->lock);
1100 #else /* RX_ENABLE_LOCKS */
1101         osi_rxSleep(&call->tq);
1102 #endif /* RX_ENABLE_LOCKS */
1103     }
1104 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1105
1106     if (call->error) {
1107         for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1108             queue_Remove(tp);
1109             rxi_FreePacket(tp);
1110         }
1111         if (cp) {
1112             rxi_FreePacket(cp);
1113             cp = call->currentPacket = NULL;
1114         }
1115         return 0;
1116     }
1117
1118     /* Loop through the I/O vector adjusting packet pointers.
1119      * Place full packets back onto the iovq once they are ready
1120      * to send. Set RX_PROTOCOL_ERROR if any problems are found in
1121      * the iovec. We put the loop condition at the end to ensure that
1122      * a zero length write will push a short packet. */
1123     nextio = 0;
1124     queue_Init(&tmpq);
1125     do {
1126         if (call->nFree == 0 && cp) {
1127             clock_NewTime();    /* Bogus:  need new time package */
1128             /* The 0, below, specifies that it is not the last packet: 
1129              * there will be others. PrepareSendPacket may
1130              * alter the packet length by up to
1131              * conn->securityMaxTrailerSize */
1132             hadd32(call->bytesSent, cp->length);
1133             rxi_PrepareSendPacket(call, cp, 0);
1134             queue_Append(&tmpq, cp);
1135
1136             /* The head of the iovq is now the current packet */
1137             if (nbytes) {
1138                 if (queue_IsEmpty(&call->iovq)) {
1139                     call->error = RX_PROTOCOL_ERROR;
1140                     cp = call->currentPacket = NULL;
1141                     for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1142                         queue_Remove(tp);
1143                         rxi_FreePacket(tp);
1144                     }
1145                     return 0;
1146                 }
1147                 cp = queue_First(&call->iovq, rx_packet);
1148                 queue_Remove(cp);
1149                 call->currentPacket = cp;
1150                 call->nFree = cp->length;
1151                 call->curvec = 1;
1152                 call->curpos =
1153                     (char *)cp->wirevec[1].iov_base +
1154                     call->conn->securityHeaderSize;
1155                 call->curlen =
1156                     cp->wirevec[1].iov_len - call->conn->securityHeaderSize;
1157             }
1158         }
1159
1160         if (nbytes) {
1161             /* The next iovec should point to the current position */
1162             if (iov[nextio].iov_base != call->curpos
1163                 || iov[nextio].iov_len > (int)call->curlen) {
1164                 call->error = RX_PROTOCOL_ERROR;
1165                 for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1166                     queue_Remove(tp);
1167                     rxi_FreePacket(tp);
1168                 }
1169                 if (cp) {
1170                     rxi_FreePacket(cp);
1171                     call->currentPacket = NULL;
1172                 }
1173                 return 0;
1174             }
1175             nbytes -= iov[nextio].iov_len;
1176             call->curpos += iov[nextio].iov_len;
1177             call->curlen -= iov[nextio].iov_len;
1178             call->nFree -= iov[nextio].iov_len;
1179             nextio++;
1180             if (call->curlen == 0) {
1181                 if (++call->curvec > cp->niovecs) {
1182                     call->nFree = 0;
1183                 } else {
1184                     call->curpos = (char *)cp->wirevec[call->curvec].iov_base;
1185                     call->curlen = cp->wirevec[call->curvec].iov_len;
1186                 }
1187             }
1188         }
1189     } while (nbytes && nextio < nio);
1190
1191     /* Move the packets from the temporary queue onto the transmit queue.
1192      * We may end up with more than call->twind packets on the queue. */
1193     for (queue_Scan(&tmpq, tp, nxp, rx_packet)) {
1194         queue_Remove(tp);
1195         queue_Append(&call->tq, tp);
1196     }
1197
1198     if (!(call->flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1199         rxi_Start(0, call, 0);
1200     }
1201
1202     /* Wait for the length of the transmit queue to fall below call->twind */
1203     while (!call->error && call->tnext + 1 > call->tfirst + call->twind) {
1204         clock_NewTime();
1205         call->startWait = clock_Sec();
1206 #ifdef  RX_ENABLE_LOCKS
1207         CV_WAIT(&call->cv_twind, &call->lock);
1208 #else
1209         call->flags |= RX_CALL_WAIT_WINDOW_ALLOC;
1210         osi_rxSleep(&call->twind);
1211 #endif
1212         call->startWait = 0;
1213     }
1214
1215     if (call->error) {
1216         if (cp) {
1217             rxi_FreePacket(cp);
1218             cp = call->currentPacket = NULL;
1219         }
1220         return 0;
1221     }
1222
1223     return requestCount - nbytes;
1224 }
1225
1226 int
1227 rx_WritevProc(struct rx_call *call, struct iovec *iov, int nio, int nbytes)
1228 {
1229     int bytes;
1230     SPLVAR;
1231
1232     NETPRI;
1233     AFS_RXGLOCK();
1234     MUTEX_ENTER(&call->lock);
1235     bytes = rxi_WritevProc(call, iov, nio, nbytes);
1236     MUTEX_EXIT(&call->lock);
1237     AFS_RXGUNLOCK();
1238     USERPRI;
1239     return bytes;
1240 }
1241
1242 /* Flush any buffered data to the stream, switch to read mode
1243  * (clients) or to EOF mode (servers) */
1244 void
1245 rxi_FlushWrite(register struct rx_call *call)
1246 {
1247     register struct rx_packet *cp = call->currentPacket;
1248     register struct rx_packet *tp;      /* Temporary packet pointer */
1249     register struct rx_packet *nxp;     /* Next packet pointer, for queue_Scan */
1250
1251     /* Free any packets from the last call to ReadvProc/WritevProc */
1252     for (queue_Scan(&call->iovq, tp, nxp, rx_packet)) {
1253         queue_Remove(tp);
1254         rxi_FreePacket(tp);
1255     }
1256
1257     if (call->mode == RX_MODE_SENDING) {
1258
1259         call->mode =
1260             (call->conn->type ==
1261              RX_CLIENT_CONNECTION ? RX_MODE_RECEIVING : RX_MODE_EOF);
1262
1263 #ifdef RX_KERNEL_TRACE
1264         {
1265             int glockOwner = ISAFS_GLOCK();
1266             if (!glockOwner)
1267                 AFS_GLOCK();
1268             afs_Trace3(afs_iclSetp, CM_TRACE_WASHERE, ICL_TYPE_STRING,
1269                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
1270                        call);
1271             if (!glockOwner)
1272                 AFS_GUNLOCK();
1273         }
1274 #endif
1275
1276 #ifdef AFS_GLOBAL_RXLOCK_KERNEL
1277         /* Wait until TQ_BUSY is reset before adding any
1278          * packets to the transmit queue
1279          */
1280         while (call->flags & RX_CALL_TQ_BUSY) {
1281             call->flags |= RX_CALL_TQ_WAIT;
1282 #ifdef RX_ENABLE_LOCKS
1283             CV_WAIT(&call->cv_tq, &call->lock);
1284 #else /* RX_ENABLE_LOCKS */
1285             osi_rxSleep(&call->tq);
1286 #endif /* RX_ENABLE_LOCKS */
1287         }
1288 #endif /* AFS_GLOBAL_RXLOCK_KERNEL */
1289
1290         if (cp) {
1291             /* cp->length is only supposed to be the user's data */
1292             /* cp->length was already set to (then-current) 
1293              * MaxUserDataSize or less. */
1294             cp->length -= call->nFree;
1295             call->currentPacket = (struct rx_packet *)0;
1296             call->nFree = 0;
1297         } else {
1298             cp = rxi_AllocSendPacket(call, 0);
1299             if (!cp) {
1300                 /* Mode can no longer be MODE_SENDING */
1301                 return;
1302             }
1303             cp->length = 0;
1304             cp->niovecs = 1;    /* just the header */
1305             call->nFree = 0;
1306         }
1307
1308         /* The 1 specifies that this is the last packet */
1309         hadd32(call->bytesSent, cp->length);
1310         rxi_PrepareSendPacket(call, cp, 1);
1311         queue_Append(&call->tq, cp);
1312         if (!
1313             (call->
1314              flags & (RX_CALL_FAST_RECOVER | RX_CALL_FAST_RECOVER_WAIT))) {
1315             rxi_Start(0, call, 0);
1316         }
1317     }
1318 }
1319
1320 /* Flush any buffered data to the stream, switch to read mode
1321  * (clients) or to EOF mode (servers) */
1322 void
1323 rx_FlushWrite(struct rx_call *call)
1324 {
1325     SPLVAR;
1326     NETPRI;
1327     AFS_RXGLOCK();
1328     MUTEX_ENTER(&call->lock);
1329     rxi_FlushWrite(call);
1330     MUTEX_EXIT(&call->lock);
1331     AFS_RXGUNLOCK();
1332     USERPRI;
1333 }