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