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