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