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