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