ab5411e88092b3779cd294499514bb2de43d1419
[openafs.git] / src / WINNT / afsd / cm_dcache.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 <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <winsock2.h>
15 #include <nb30.h>
16 #ifdef COMMENT
17 #include <malloc.h>
18 #endif
19 #include <string.h>
20 #include <stdlib.h>
21 #include <osi.h>
22
23 #include "afsd.h"
24
25 #ifdef DEBUG
26 extern void afsi_log(char *pattern, ...);
27 #endif
28
29 #ifdef AFS_FREELANCE_CLIENT
30 extern osi_mutex_t cm_Freelance_Lock;
31 #endif
32
33 #ifdef AFS_LARGEFILES
34 /* we can access connp->serverp without holding a lock because that
35    never changes since the connection is made. */
36 #define SERVERHAS64BIT(connp) (!((connp)->serverp->flags & CM_SERVERFLAG_NO64BIT))
37 #define SET_SERVERHASNO64BIT(connp) (cm_SetServerNo64Bit((connp)->serverp, TRUE))
38 #else
39 #define SERVERHAS64BIT(connp) (FALSE)
40 #define SET_SERVERHASNO64BIT(connp) (FALSE)
41 #endif
42
43 /* functions called back from the buffer package when reading or writing data,
44  * or when holding or releasing a vnode pointer.
45  */
46 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
47                  cm_user_t *userp, cm_req_t *reqp)
48 {
49     /* store the data back from this buffer; the buffer is locked and held,
50      * but the vnode involved isn't locked, yet.  It is held by its
51      * reference from the buffer, which won't change until the buffer is
52      * released by our caller.  Thus, we don't have to worry about holding
53      * bufp->scp.
54      */
55     long code, code1;
56     cm_scache_t *scp = vscp;
57     afs_int32 nbytes;
58 #ifdef AFS_LARGEFILES
59     afs_int32 save_nbytes;
60 #endif
61     long temp;
62     AFSFetchStatus outStatus;
63     AFSStoreStatus inStatus;
64     osi_hyper_t thyper;
65     AFSVolSync volSync;
66     AFSFid tfid;
67     struct rx_call *rxcallp;
68     struct rx_connection *rxconnp;
69     osi_queueData_t *qdp;
70     cm_buf_t *bufp;
71     afs_uint32 wbytes;
72     char *bufferp;
73     cm_conn_t *connp;
74     osi_hyper_t truncPos;
75     cm_bulkIO_t biod;           /* bulk IO descriptor */
76     int require_64bit_ops = 0;
77     int call_was_64bit = 0;
78
79     osi_assertx(userp != NULL, "null cm_user_t");
80     osi_assertx(scp != NULL, "null cm_scache_t");
81
82     /* now, the buffer may or may not be filled with good data (buf_GetNew
83      * drops lots of locks, and may indeed return a properly initialized
84      * buffer, although more likely it will just return a new, empty, buffer.
85      */
86
87     lock_ObtainWrite(&scp->rw);
88     if (scp->flags & CM_SCACHEFLAG_DELETED) {
89         lock_ReleaseWrite(&scp->rw);
90         return CM_ERROR_NOSUCHFILE;
91     }
92
93     cm_AFSFidFromFid(&tfid, &scp->fid);
94
95     /* Serialize StoreData RPC's; for rationale see cm_scache.c */
96     (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
97
98     code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
99     if (code) {
100         osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
101         cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
102         lock_ReleaseWrite(&scp->rw);
103         return code;
104     }
105
106     if (biod.length == 0) {
107         osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
108         cm_ReleaseBIOD(&biod, 1, 0, 1); /* should be a NOOP */
109         cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
110         lock_ReleaseWrite(&scp->rw);
111         return 0;
112     }
113
114     /* prepare the output status for the store */
115     scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
116     cm_StatusFromAttr(&inStatus, scp, NULL);
117     truncPos = scp->length;
118     if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
119         && LargeIntegerLessThan(scp->truncPos, truncPos))
120         truncPos = scp->truncPos;
121         scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
122                 
123     /* compute how many bytes to write from this buffer */
124     thyper = LargeIntegerSubtract(scp->length, biod.offset);
125     if (LargeIntegerLessThanZero(thyper)) {
126         /* entire buffer is past EOF */
127         nbytes = 0;
128     }
129     else {
130         /* otherwise write out part of buffer before EOF, but not
131          * more than bufferSize bytes.
132          */
133         if (LargeIntegerGreaterThan(thyper,
134                                     ConvertLongToLargeInteger(biod.length))) {
135             nbytes = biod.length;
136         } else {
137             /* if thyper is less than or equal to biod.length, then we
138                can safely assume that the value fits in a long. */
139             nbytes = thyper.LowPart;
140         }
141     }
142
143     if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
144                                                  ConvertLongToLargeInteger(nbytes)),
145                                  ConvertLongToLargeInteger(LONG_MAX)) ||
146          LargeIntegerGreaterThan(truncPos,
147                                  ConvertLongToLargeInteger(LONG_MAX))) {
148         require_64bit_ops = 1;
149     }
150         
151     lock_ReleaseWrite(&scp->rw);
152
153     /* now we're ready to do the store operation */
154 #ifdef AFS_LARGEFILES
155     save_nbytes = nbytes;
156 #endif
157     do {
158         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
159         if (code) 
160             continue;
161
162     retry:
163         rxconnp = cm_GetRxConn(connp);
164         rxcallp = rx_NewCall(rxconnp);
165         rx_PutConnection(rxconnp);
166
167 #ifdef AFS_LARGEFILES
168         if (SERVERHAS64BIT(connp)) {
169             call_was_64bit = 1;
170
171             osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
172                      scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
173
174             code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
175                                           biod.offset.QuadPart,
176                                           nbytes,
177                                           truncPos.QuadPart);
178             if (code)
179                 osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData64 FAILURE, code 0x%x", code);
180             else
181                 osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
182         } else {
183             call_was_64bit = 0;
184
185             if (require_64bit_ops) {
186                 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData.  The operation requires large file support in the server.");
187                 code = CM_ERROR_TOOBIG;
188             } else {
189                 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
190                          scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
191
192                 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
193                                             biod.offset.LowPart, nbytes, truncPos.LowPart);
194                 if (code)
195                     osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
196                 else
197                     osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
198             }
199         }
200 #else
201         osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
202                  scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
203
204         code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
205                                     biod.offset.LowPart, nbytes, truncPos.LowPart);
206         if (code)
207             osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
208         else
209             osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData SUCCESS");
210 #endif
211
212         if (code == 0) {
213             /* write the data from the the list of buffers */
214             qdp = NULL;
215             while(nbytes > 0) {
216                 afs_uint32 buf_offset;
217                 if (qdp == NULL) {
218                     qdp = biod.bufListEndp;
219                     buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
220                 } else {
221                     qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
222                     buf_offset = 0;
223                 }
224                 osi_assertx(qdp != NULL, "null osi_queueData_t");
225                 bufp = osi_GetQData(qdp);
226                 bufferp = bufp->datap + buf_offset;
227                 wbytes = nbytes;
228                 if (wbytes > cm_data.buf_blockSize - buf_offset)
229                     wbytes = cm_data.buf_blockSize - buf_offset;
230
231                 /* write out wbytes of data from bufferp */
232                 temp = rx_Write(rxcallp, bufferp, wbytes);
233                 if (temp != wbytes) {
234                     osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d",bufp,temp,wbytes);
235                     code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
236                     break;
237                 } else {
238                     osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p, %d",bufp,temp);
239                 }       
240                 nbytes -= wbytes;
241             }   /* while more bytes to write */
242         }       /* if RPC started successfully */
243
244         if (code == 0) {
245             if (call_was_64bit) {
246                 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
247                 if (code)
248                     osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
249                 else
250                     osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
251             } else {
252                 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
253                 if (code)
254                     osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
255                 else
256                     osi_Log0(afsd_logp, "EndRXAFS_StoreData SUCCESS");
257             }
258         }
259
260         code1 = rx_EndCall(rxcallp, code);
261
262 #ifdef AFS_LARGEFILES
263         if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
264             SET_SERVERHASNO64BIT(connp);
265             qdp = NULL;
266             nbytes = save_nbytes;
267             goto retry;
268         }
269 #endif
270         /* Prefer StoreData error over rx_EndCall error */
271         if (code1 != 0)
272             code = code1;
273     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
274
275     code = cm_MapRPCError(code, reqp);
276
277     if (code)
278         osi_Log2(afsd_logp, "CALL StoreData FAILURE scp 0x%p, code 0x%x", scp, code);
279     else
280         osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
281
282     /* now, clean up our state */
283     lock_ObtainWrite(&scp->rw);
284
285     cm_ReleaseBIOD(&biod, 1, code, 1);
286     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
287
288     if (code == 0) {
289         osi_hyper_t t;
290         /* now, here's something a little tricky: in AFS 3, a dirty
291          * length can't be directly stored, instead, a dirty chunk is
292          * stored that sets the file's size (by writing and by using
293          * the truncate-first option in the store call).
294          *
295          * At this point, we've just finished a store, and so the trunc
296          * pos field is clean.  If the file's size at the server is at
297          * least as big as we think it should be, then we turn off the
298          * length dirty bit, since all the other dirty buffers must
299          * precede this one in the file.
300          *
301          * The file's desired size shouldn't be smaller than what's
302          * stored at the server now, since we just did the trunc pos
303          * store.
304          *
305          * We have to turn off the length dirty bit as soon as we can,
306          * so that we see updates made by other machines.
307          */
308
309         if (call_was_64bit) {
310             t.LowPart = outStatus.Length;
311             t.HighPart = outStatus.Length_hi;
312         } else {
313             t = ConvertLongToLargeInteger(outStatus.Length);
314         }
315
316         if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
317             scp->mask &= ~CM_SCACHEMASK_LENGTH;
318
319         cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
320     } else {
321         if (code == CM_ERROR_SPACE)
322             scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
323         else if (code == CM_ERROR_QUOTA)
324             scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
325     }
326     lock_ReleaseWrite(&scp->rw);
327
328     return code;
329 }
330
331 /*
332  * Truncate the file, by sending a StoreData RPC with zero length.
333  *
334  * Called with scp locked.  Releases and re-obtains the lock.
335  */
336 long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
337 {
338     AFSFetchStatus outStatus;
339     AFSStoreStatus inStatus;
340     AFSVolSync volSync;
341     AFSFid tfid;
342     long code, code1;
343     osi_hyper_t truncPos;
344     cm_conn_t *connp;
345     struct rx_call *rxcallp;
346     struct rx_connection *rxconnp;
347     int require_64bit_ops = 0;
348     int call_was_64bit = 0;
349
350     /* Serialize StoreData RPC's; for rationale see cm_scache.c */
351     (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
352                      CM_SCACHESYNC_STOREDATA_EXCL);
353
354     /* prepare the output status for the store */
355     inStatus.Mask = AFS_SETMODTIME;
356     inStatus.ClientModTime = scp->clientModTime;
357     scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
358
359     /* calculate truncation position */
360     truncPos = scp->length;
361     if ((scp->mask & CM_SCACHEMASK_TRUNCPOS)
362         && LargeIntegerLessThan(scp->truncPos, truncPos))
363         truncPos = scp->truncPos;
364     scp->mask &= ~CM_SCACHEMASK_TRUNCPOS;
365
366     if (LargeIntegerGreaterThan(truncPos,
367                                 ConvertLongToLargeInteger(LONG_MAX))) {
368
369         require_64bit_ops = 1;
370     }
371
372     lock_ReleaseWrite(&scp->rw);
373
374     cm_AFSFidFromFid(&tfid, &scp->fid);
375
376     /* now we're ready to do the store operation */
377     do {
378         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
379         if (code) 
380             continue;
381
382     retry:      
383         rxconnp = cm_GetRxConn(connp);
384         rxcallp = rx_NewCall(rxconnp);
385         rx_PutConnection(rxconnp);
386
387 #ifdef AFS_LARGEFILES
388         if (SERVERHAS64BIT(connp)) {
389             call_was_64bit = 1;
390
391             code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
392                                           0, 0, truncPos.QuadPart);
393         } else {
394             call_was_64bit = 0;
395
396             if (require_64bit_ops) {
397                 code = CM_ERROR_TOOBIG;
398             } else {
399                 code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
400                                             0, 0, truncPos.LowPart);
401             }
402         }
403 #else
404         code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
405                                     0, 0, truncPos.LowPart);
406 #endif
407
408         if (code == 0) {
409             if (call_was_64bit)
410                 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
411             else
412                 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
413         }
414         code1 = rx_EndCall(rxcallp, code);
415
416 #ifdef AFS_LARGEFILES
417         if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
418             SET_SERVERHASNO64BIT(connp);
419             goto retry;
420         }
421 #endif
422         /* prefer StoreData error over rx_EndCall error */
423         if (code == 0 && code1 != 0)
424             code = code1;
425     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
426     code = cm_MapRPCError(code, reqp);
427         
428     /* now, clean up our state */
429     lock_ObtainWrite(&scp->rw);
430
431     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
432
433     if (code == 0) {
434         osi_hyper_t t;
435         /*
436          * For explanation of handling of CM_SCACHEMASK_LENGTH,
437          * see cm_BufWrite().
438          */
439         if (call_was_64bit) {
440             t.HighPart = outStatus.Length_hi;
441             t.LowPart = outStatus.Length;
442         } else {
443             t = ConvertLongToLargeInteger(outStatus.Length);
444         }
445
446         if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
447             scp->mask &= ~CM_SCACHEMASK_LENGTH;
448         cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
449     }
450
451     return code;
452 }
453
454 long cm_BufRead(cm_buf_t *bufp, long nbytes, long *bytesReadp, cm_user_t *userp)
455 {
456     *bytesReadp = cm_data.buf_blockSize;
457
458     /* now return a code that means that I/O is done */
459     return 0;
460 }
461
462 /* stabilize scache entry, and return with it locked so 
463  * it stays stable.
464  */
465 long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
466 {
467     cm_scache_t *scp = vscp;
468     long code;
469
470     lock_ObtainWrite(&scp->rw);
471     code = cm_SyncOp(scp, NULL, userp, reqp, 0, 
472                      CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
473     if (code) {
474         lock_ReleaseWrite(&scp->rw);
475         return code;
476     }
477         
478     return 0;
479 }
480
481 /* undoes the work that cm_BufStabilize does: releases lock so things can change again */
482 long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
483 {
484     cm_scache_t *scp = vscp;
485         
486     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
487
488     lock_ReleaseWrite(&scp->rw);
489         
490     /* always succeeds */
491     return 0;
492 }
493
494 cm_buf_ops_t cm_bufOps = {
495     cm_BufWrite,
496     cm_BufRead,
497     cm_BufStabilize,
498     cm_BufUnstabilize
499 };
500
501 long cm_ValidateDCache(void)
502 {
503     return buf_ValidateBuffers();
504 }
505
506 long cm_ShutdownDCache(void)
507 {
508     return 0;
509 }
510
511 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
512 {
513     return buf_Init(newFile, &cm_bufOps, nbuffers);
514 }
515
516 /* check to see if we have an up-to-date buffer.  The buffer must have
517  * previously been obtained by calling buf_Get.
518  *
519  * Make sure we have a callback, and that the dataversion matches.
520  *
521  * Scp must be locked.
522  *
523  * Bufp *may* be locked.
524  */
525 int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
526 {
527     int code;
528     if (!cm_HaveCallback(scp))
529         return 0;
530     if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
531         return 1;
532     if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
533         return 1;
534     if (!isBufLocked) {
535         code = lock_TryMutex(&bufp->mx);
536         if (code == 0) {
537             /* don't have the lock, and can't lock it, then
538              * return failure.
539              */
540             return 0;
541         }
542     }
543
544     /* remember dirty flag for later */
545     code = bufp->flags & CM_BUF_DIRTY;
546
547     /* release lock if we obtained it here */
548     if (!isBufLocked) 
549         lock_ReleaseMutex(&bufp->mx);
550
551     /* if buffer was dirty, buffer is acceptable for use */
552     if (code) 
553         return 1;
554     else 
555         return 0;
556 }
557
558 /* used when deciding whether to do a prefetch or not */
559 long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *length,
560                         cm_user_t *userp, cm_req_t *reqp, osi_hyper_t *realBasep)
561 {
562     osi_hyper_t tbase;
563     osi_hyper_t tlength;
564     osi_hyper_t tblocksize;
565     long code;
566     cm_buf_t *bp;
567     int stop;
568         
569     /* now scan all buffers in the range, looking for any that look like
570      * they need work.
571      */
572     tbase = *startBasep;
573     tlength = *length;
574     tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
575     stop = 0;
576     lock_ObtainWrite(&scp->rw);
577     while (LargeIntegerGreaterThanZero(tlength)) {
578         /* get callback so we can do a meaningful dataVersion comparison */
579         code = cm_SyncOp(scp, NULL, userp, reqp, 0,
580                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
581         if (code) {
582             scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
583             lock_ReleaseWrite(&scp->rw);
584             return code;
585         }
586                 
587         if (LargeIntegerGreaterThanOrEqualTo(tbase, scp->length)) {
588             /* we're past the end of file */
589             break;
590         }
591
592         bp = buf_Find(scp, &tbase);
593         /* We cheat slightly by not locking the bp mutex. */
594         if (bp) {
595             if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
596                  && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
597                 stop = 1;
598             buf_Release(bp);
599             bp = NULL;
600         }
601         else 
602             stop = 1;
603
604         /* if this buffer is essentially guaranteed to require a fetch,
605          * break out here and return this position.
606          */
607         if (stop) 
608             break;
609                 
610         tbase = LargeIntegerAdd(tbase, tblocksize);
611         tlength = LargeIntegerSubtract(tlength,  tblocksize);
612     }
613         
614     /* if we get here, either everything is fine or 'stop' stopped us at a
615      * particular buffer in the range that definitely needs to be fetched.
616      */
617     if (stop == 0) {
618         /* return non-zero code since realBasep won't be valid */
619         scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
620         code = -1;
621     }   
622     else {
623         /* successfully found a page that will need fetching */
624         *realBasep = tbase;
625         code = 0;
626     }
627     lock_ReleaseWrite(&scp->rw);
628     return code;
629 }
630
631 afs_int32
632 cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
633             cm_user_t *userp)
634 {
635     osi_hyper_t toffset;
636     long length;
637     cm_req_t req;
638     long code = 0;
639
640     if (scp->flags & CM_SCACHEFLAG_DELETED) {
641         osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
642     } else {
643         cm_InitReq(&req);
644
645         /* Retries will be performed by the BkgDaemon thread if appropriate */
646         req.flags |= CM_REQ_NORETRY;
647
648         toffset.LowPart = p1;
649         toffset.HighPart = p2;
650         length = p3;
651
652         osi_Log4(afsd_logp, "Starting BKG store scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
653
654         code = cm_BufWrite(scp, &toffset, length, /* flags */ 0, userp, &req);
655
656         osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
657     }
658
659     /* 
660      * Keep the following list synchronized with the
661      * error code list in cm_BkgDaemon 
662      */
663     switch ( code ) {
664     case CM_ERROR_TIMEDOUT: /* or server restarting */
665     case CM_ERROR_RETRY:
666     case CM_ERROR_WOULDBLOCK:
667     case CM_ERROR_ALLBUSY:
668     case CM_ERROR_ALLDOWN:
669     case CM_ERROR_ALLOFFLINE:
670     case CM_ERROR_PARTIALWRITE:
671         break;  /* cm_BkgDaemon will re-insert the request in the queue */
672     case 0:
673     default:
674         lock_ObtainWrite(&scp->rw);
675         cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
676         lock_ReleaseWrite(&scp->rw);
677     }
678     return code;
679 }
680
681 /* Called with scp locked */
682 void cm_ClearPrefetchFlag(long code, cm_scache_t *scp, osi_hyper_t *base, osi_hyper_t *length)
683 {
684     osi_hyper_t end;
685
686     if (code == 0) {
687         end =  LargeIntegerAdd(*base, *length);
688         if (LargeIntegerGreaterThan(*base, scp->prefetch.base))
689             scp->prefetch.base = *base;
690         if (LargeIntegerGreaterThan(end, scp->prefetch.end))
691             scp->prefetch.end = end;
692     }
693     scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
694 }
695
696 /* do the prefetch.  if the prefetch fails, return 0 (success)
697  * because there is no harm done.  */
698 afs_int32
699 cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
700                cm_user_t *userp)
701 {
702     osi_hyper_t length;
703     osi_hyper_t base;
704     osi_hyper_t offset;
705     osi_hyper_t end;
706     osi_hyper_t fetched;
707     osi_hyper_t tblocksize;
708     long code;
709     int mxheld = 0;
710     cm_buf_t *bp = NULL;
711     cm_req_t req;
712
713     cm_InitReq(&req);
714
715     /* Retries will be performed by the BkgDaemon thread if appropriate */
716     req.flags |= CM_REQ_NORETRY;
717         
718     fetched.LowPart = 0;
719     fetched.HighPart = 0;
720     tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
721     base.LowPart = p1;
722     base.HighPart = p2;
723     length.LowPart = p3;
724     length.HighPart = p4;
725
726     end = LargeIntegerAdd(base, length);
727         
728     osi_Log3(afsd_logp, "Starting BKG prefetch scp 0x%p, base 0x%x:%x", scp, p2, p1);
729
730     for ( code = 0, offset = base;
731           code == 0 && LargeIntegerLessThan(offset, end); 
732           offset = LargeIntegerAdd(offset, tblocksize) )
733     {
734         if (mxheld) {
735             lock_ReleaseWrite(&scp->rw);
736             mxheld = 0;
737         }
738
739         code = buf_Get(scp, &offset, &req, &bp);
740         if (code)
741             break;
742
743         if (bp->cmFlags & CM_BUF_CMFETCHING) {
744             /* skip this buffer as another thread is already fetching it */
745             buf_Release(bp);
746             bp = NULL;
747             continue;
748         }
749
750         if (!mxheld) {
751             lock_ObtainWrite(&scp->rw);
752             mxheld = 1;
753         }
754
755         code = cm_GetBuffer(scp, bp, NULL, userp, &req);
756         if (code == 0)
757             fetched = LargeIntegerAdd(fetched, tblocksize); 
758         buf_Release(bp);
759     }
760     
761     if (!mxheld) {
762         lock_ObtainWrite(&scp->rw);
763         mxheld = 1;
764     }
765     cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code, 
766                          scp, &base, &fetched);
767     lock_ReleaseWrite(&scp->rw);
768
769     osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p, code %d bytes 0x%x:%x", 
770               scp, code, fetched.HighPart, fetched.LowPart);
771     return code;
772 }
773
774 /* a read was issued to offsetp, and we have to determine whether we should
775  * do a prefetch of the next chunk.
776  */
777 void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
778                          cm_user_t *userp, cm_req_t *reqp)
779 {
780     long code;
781     osi_hyper_t realBase;
782     osi_hyper_t readBase;
783     osi_hyper_t readLength;
784         
785     readBase = *offsetp;
786     /* round up to chunk boundary */
787     readBase.LowPart += (cm_chunkSize-1);
788     readBase.LowPart &= (-cm_chunkSize);
789
790     readLength = ConvertLongToLargeInteger(count);
791
792     lock_ObtainWrite(&scp->rw);
793     if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
794          || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
795         lock_ReleaseWrite(&scp->rw);
796         return;
797     }
798     scp->flags |= CM_SCACHEFLAG_PREFETCHING;
799
800     /* start the scan at the latter of the end of this read or
801      * the end of the last fetched region.
802      */
803     if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
804         readBase = scp->prefetch.end;
805
806     lock_ReleaseWrite(&scp->rw);
807
808     code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
809                               &realBase);
810     if (code) 
811         return; /* can't find something to prefetch */
812
813     osi_Log2(afsd_logp, "BKG Prefetch request scp 0x%p, base 0x%x",
814              scp, realBase.LowPart);
815
816     cm_QueueBKGRequest(scp, cm_BkgPrefetch, 
817                        realBase.LowPart, realBase.HighPart, 
818                        readLength.LowPart, readLength.HighPart, 
819                        userp);
820 }
821
822 /* scp must be locked; temporarily unlocked during processing.
823  * If returns 0, returns buffers held in biop, and with
824  * CM_BUF_CMSTORING set.
825  *
826  * Caller *must* set CM_BUF_WRITING and reset the over.hEvent field if the
827  * buffer is ever unlocked before CM_BUF_DIRTY is cleared.  And if
828  * CM_BUF_WRITING is ever viewed by anyone, then it must be cleared, sleepers
829  * must be woken, and the event must be set when the I/O is done.  All of this
830  * is required so that buf_WaitIO synchronizes properly with the buffer as it
831  * is being written out.
832  */
833 long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
834                        cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
835 {
836     cm_buf_t *bufp;
837     osi_queueData_t *qdp;
838     osi_hyper_t thyper;
839     osi_hyper_t tbase;
840     osi_hyper_t scanStart;              /* where to start scan for dirty pages */
841     osi_hyper_t scanEnd;                /* where to stop scan for dirty pages */
842     osi_hyper_t firstModOffset; /* offset of first modified page in range */
843     long temp;
844     long code;
845     long flags;                 /* flags to cm_SyncOp */
846         
847     /* clear things out */
848     biop->scp = scp;                    /* do not hold; held by caller */
849     biop->offset = *inOffsetp;
850     biop->length = 0;
851     biop->bufListp = NULL;
852     biop->bufListEndp = NULL;
853     biop->reserved = 0;
854
855     /* reserve a chunk's worth of buffers */
856     lock_ReleaseWrite(&scp->rw);
857     buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
858     lock_ObtainWrite(&scp->rw);
859
860     bufp = NULL;
861     for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
862         thyper = ConvertLongToLargeInteger(temp);
863         tbase = LargeIntegerAdd(*inOffsetp, thyper);
864
865         bufp = buf_Find(scp, &tbase);
866         if (bufp) {
867             /* get buffer mutex and scp mutex safely */
868             lock_ReleaseWrite(&scp->rw);
869             lock_ObtainMutex(&bufp->mx);
870             lock_ObtainWrite(&scp->rw);
871
872             flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
873             code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags); 
874             if (code) {
875                 lock_ReleaseMutex(&bufp->mx);
876                 buf_Release(bufp);
877                 bufp = NULL;
878                 buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
879                 return code;
880             }   
881                         
882             /* if the buffer is dirty, we're done */
883             if (bufp->flags & CM_BUF_DIRTY) {
884                 osi_assertx(!(bufp->flags & CM_BUF_WRITING),
885                             "WRITING w/o CMSTORING in SetupStoreBIOD");
886                 bufp->flags |= CM_BUF_WRITING;
887                 break;
888             }
889
890             /* this buffer is clean, so there's no reason to process it */
891             cm_SyncOpDone(scp, bufp, flags);
892             lock_ReleaseMutex(&bufp->mx);
893             buf_Release(bufp);
894             bufp = NULL;
895         }       
896     }
897
898     biop->reserved = 1;
899         
900     /* if we get here, if bufp is null, we didn't find any dirty buffers
901      * that weren't already being stored back, so we just quit now.
902      */
903     if (!bufp) {
904         return 0;
905     }
906
907     /* don't need buffer mutex any more */
908     lock_ReleaseMutex(&bufp->mx);
909         
910     /* put this element in the list */
911     qdp = osi_QDAlloc();
912     osi_SetQData(qdp, bufp);
913     /* don't have to hold bufp, since held by buf_Find above */
914     osi_QAddH((osi_queue_t **) &biop->bufListp,
915               (osi_queue_t **) &biop->bufListEndp,
916               &qdp->q);
917     biop->length = cm_data.buf_blockSize;
918     firstModOffset = bufp->offset;
919     biop->offset = firstModOffset;
920     bufp = NULL;        /* this buffer and reference added to the queue */
921
922     /* compute the window surrounding *inOffsetp of size cm_chunkSize */
923     scanStart = *inOffsetp;
924     scanStart.LowPart &= (-cm_chunkSize);
925     thyper = ConvertLongToLargeInteger(cm_chunkSize);
926     scanEnd = LargeIntegerAdd(scanStart, thyper);
927
928     flags = CM_SCACHESYNC_GETSTATUS
929         | CM_SCACHESYNC_STOREDATA
930         | CM_SCACHESYNC_BUFLOCKED
931         | CM_SCACHESYNC_NOWAIT;
932
933     /* start by looking backwards until scanStart */
934     /* hyper version of cm_data.buf_blockSize */
935     thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
936     tbase = LargeIntegerSubtract(firstModOffset, thyper);
937     while(LargeIntegerGreaterThanOrEqualTo(tbase, scanStart)) {
938         /* see if we can find the buffer */
939         bufp = buf_Find(scp, &tbase);
940         if (!bufp) 
941             break;
942
943         /* try to lock it, and quit if we can't (simplifies locking) */
944         lock_ReleaseWrite(&scp->rw);
945         code = lock_TryMutex(&bufp->mx);
946         lock_ObtainWrite(&scp->rw);
947         if (code == 0) {
948             buf_Release(bufp);
949             bufp = NULL;
950             break;
951         }
952                 
953         code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
954         if (code) {
955             lock_ReleaseMutex(&bufp->mx);
956             buf_Release(bufp);
957             bufp = NULL;
958             break;
959         }
960                 
961         if (!(bufp->flags & CM_BUF_DIRTY)) {
962             /* buffer is clean, so we shouldn't add it */
963             cm_SyncOpDone(scp, bufp, flags);
964             lock_ReleaseMutex(&bufp->mx);
965             buf_Release(bufp);
966             bufp = NULL;
967             break;
968         }
969
970         /* don't need buffer mutex any more */
971         lock_ReleaseMutex(&bufp->mx);
972
973         /* we have a dirty buffer ready for storing.  Add it to the tail
974          * of the list, since it immediately precedes all of the disk
975          * addresses we've already collected.
976          */
977         qdp = osi_QDAlloc();
978         osi_SetQData(qdp, bufp);
979         /* no buf_hold necessary, since we have it held from buf_Find */
980         osi_QAddT((osi_queue_t **) &biop->bufListp,
981                   (osi_queue_t **) &biop->bufListEndp,
982                   &qdp->q);
983         bufp = NULL;            /* added to the queue */
984
985         /* update biod info describing the transfer */
986         biop->offset = LargeIntegerSubtract(biop->offset, thyper);
987         biop->length += cm_data.buf_blockSize;
988
989         /* update loop pointer */
990         tbase = LargeIntegerSubtract(tbase, thyper);
991     }   /* while loop looking for pages preceding the one we found */
992
993     /* now, find later dirty, contiguous pages, and add them to the list */
994     /* hyper version of cm_data.buf_blockSize */
995     thyper = ConvertLongToLargeInteger(cm_data.buf_blockSize);
996     tbase = LargeIntegerAdd(firstModOffset, thyper);
997     while(LargeIntegerLessThan(tbase, scanEnd)) {
998         /* see if we can find the buffer */
999         bufp = buf_Find(scp, &tbase);
1000         if (!bufp) 
1001             break;
1002
1003         /* try to lock it, and quit if we can't (simplifies locking) */
1004         lock_ReleaseWrite(&scp->rw);
1005         code = lock_TryMutex(&bufp->mx);
1006         lock_ObtainWrite(&scp->rw);
1007         if (code == 0) {
1008             buf_Release(bufp);
1009             bufp = NULL;
1010             break;
1011         }
1012
1013         code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags);
1014         if (code) {
1015             lock_ReleaseMutex(&bufp->mx);
1016             buf_Release(bufp);
1017             bufp = NULL;
1018             break;
1019         }
1020                 
1021         if (!(bufp->flags & CM_BUF_DIRTY)) {
1022             /* buffer is clean, so we shouldn't add it */
1023             cm_SyncOpDone(scp, bufp, flags);
1024             lock_ReleaseMutex(&bufp->mx);
1025             buf_Release(bufp);
1026             bufp = NULL;
1027             break;
1028         }
1029
1030         /* don't need buffer mutex any more */
1031         lock_ReleaseMutex(&bufp->mx);
1032
1033         /* we have a dirty buffer ready for storing.  Add it to the head
1034          * of the list, since it immediately follows all of the disk
1035          * addresses we've already collected.
1036          */
1037         qdp = osi_QDAlloc();
1038         osi_SetQData(qdp, bufp);
1039         /* no buf_hold necessary, since we have it held from buf_Find */
1040         osi_QAddH((osi_queue_t **) &biop->bufListp,
1041                   (osi_queue_t **) &biop->bufListEndp,
1042                   &qdp->q);
1043         bufp = NULL;
1044
1045         /* update biod info describing the transfer */
1046         biop->length += cm_data.buf_blockSize;
1047                 
1048         /* update loop pointer */
1049         tbase = LargeIntegerAdd(tbase, thyper);
1050     }   /* while loop looking for pages following the first page we found */
1051         
1052     /* finally, we're done */
1053     return 0;
1054 }
1055
1056 /* scp must be locked; temporarily unlocked during processing.
1057  * If returns 0, returns buffers held in biop, and with
1058  * CM_BUF_CMFETCHING flags set.
1059  * If an error is returned, we don't return any buffers.
1060  */
1061 long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
1062                         cm_bulkIO_t *biop, cm_user_t *userp, cm_req_t *reqp)
1063 {
1064     long code;
1065     cm_buf_t *tbp;
1066     osi_hyper_t tblocksize;             /* a long long temp variable */
1067     osi_hyper_t pageBase;               /* base offset we're looking at */
1068     osi_queueData_t *qdp;               /* one temp queue structure */
1069     osi_queueData_t *tqdp;              /* another temp queue structure */
1070     long collected;                     /* how many bytes have been collected */
1071     int isFirst;
1072     long flags;
1073     osi_hyper_t fileSize;               /* the # of bytes in the file */
1074     osi_queueData_t *heldBufListp;      /* we hold all buffers in this list */
1075     osi_queueData_t *heldBufListEndp;   /* first one */
1076     int reserving;
1077
1078     tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
1079
1080     biop->scp = scp;                    /* do not hold; held by caller */
1081     biop->offset = *offsetp;
1082     /* null out the list of buffers */
1083     biop->bufListp = biop->bufListEndp = NULL;
1084     biop->reserved = 0;
1085
1086     /* first lookup the file's length, so we know when to stop */
1087     code = cm_SyncOp(scp, NULL, userp, reqp, 0, 
1088                      CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1089     if (code) 
1090         return code;
1091         
1092     /* copy out size, since it may change */
1093     fileSize = scp->serverLength;
1094         
1095     lock_ReleaseWrite(&scp->rw);
1096
1097     pageBase = *offsetp;
1098     collected = pageBase.LowPart & (cm_chunkSize - 1);
1099     heldBufListp = NULL;
1100     heldBufListEndp = NULL;
1101
1102     /*
1103      * Obtaining buffers can cause dirty buffers to be recycled, which
1104      * can cause a storeback, so cannot be done while we have buffers
1105      * reserved.
1106      *
1107      * To get around this, we get buffers twice.  Before reserving buffers,
1108      * we obtain and release each one individually.  After reserving
1109      * buffers, we try to obtain them again, but only by lookup, not by
1110      * recycling.  If a buffer has gone away while we were waiting for
1111      * the others, we just use whatever buffers we already have.
1112      *
1113      * On entry to this function, we are already holding a buffer, so we
1114      * can't wait for reservation.  So we call buf_TryReserveBuffers()
1115      * instead.  Not only that, we can't really even call buf_Get(), for
1116      * the same reason.  We can't avoid that, though.  To avoid deadlock
1117      * we allow only one thread to be executing the buf_Get()-buf_Release()
1118      * sequence at a time.
1119      */
1120
1121     /* first hold all buffers, since we can't hold any locks in buf_Get */
1122     while (1) {
1123         /* stop at chunk boundary */
1124         if (collected >= cm_chunkSize) 
1125             break;
1126                 
1127         /* see if the next page would be past EOF */
1128         if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) 
1129             break;
1130
1131         code = buf_Get(scp, &pageBase, reqp, &tbp);
1132         if (code) {
1133             lock_ObtainWrite(&scp->rw);
1134             cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1135             return code;
1136         }
1137                 
1138         buf_Release(tbp);
1139         tbp = NULL;
1140
1141         pageBase = LargeIntegerAdd(tblocksize, pageBase);
1142         collected += cm_data.buf_blockSize;
1143     }
1144
1145     /* reserve a chunk's worth of buffers if possible */
1146     reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1147
1148     pageBase = *offsetp;
1149     collected = pageBase.LowPart & (cm_chunkSize - 1);
1150
1151     /* now hold all buffers, if they are still there */
1152     while (1) {
1153         /* stop at chunk boundary */
1154         if (collected >= cm_chunkSize) 
1155             break;
1156                 
1157         /* see if the next page would be past EOF */
1158         if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) 
1159             break;
1160
1161         tbp = buf_Find(scp, &pageBase);
1162         if (!tbp) 
1163             break;
1164
1165         /* add the buffer to the list */
1166         qdp = osi_QDAlloc();
1167         osi_SetQData(qdp, tbp);
1168         osi_QAddH((osi_queue_t **)&heldBufListp, 
1169                   (osi_queue_t **)&heldBufListEndp, 
1170                   &qdp->q);
1171         /* leave tbp held (from buf_Get) */
1172
1173         if (!reserving) 
1174             break;
1175
1176         collected += cm_data.buf_blockSize;
1177         pageBase = LargeIntegerAdd(tblocksize, pageBase);
1178     }
1179
1180     /* look at each buffer, adding it into the list if it looks idle and
1181      * filled with old data.  One special case: wait for idle if it is the
1182      * first buffer since we really need that one for our caller to make
1183      * any progress.
1184      */
1185     isFirst = 1;
1186     collected = 0;              /* now count how many we'll really use */
1187     for (tqdp = heldBufListEndp;
1188         tqdp;
1189           tqdp = (osi_queueData_t *) osi_QPrev(&tqdp->q)) {
1190         /* get a ptr to the held buffer */
1191         tbp = osi_GetQData(tqdp);
1192         pageBase = tbp->offset;
1193
1194         /* now lock the buffer lock */
1195         lock_ObtainMutex(&tbp->mx);
1196         lock_ObtainWrite(&scp->rw);
1197
1198         /* don't bother fetching over data that is already current */
1199         if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
1200             /* we don't need this buffer, since it is current */
1201             lock_ReleaseWrite(&scp->rw);
1202             lock_ReleaseMutex(&tbp->mx);
1203             break;
1204         }
1205
1206         flags = CM_SCACHESYNC_FETCHDATA | CM_SCACHESYNC_BUFLOCKED;
1207         if (!isFirst) 
1208             flags |= CM_SCACHESYNC_NOWAIT;
1209
1210         /* wait for the buffer to serialize, if required.  Doesn't
1211          * release the scp or buffer lock(s) if NOWAIT is specified.
1212          */
1213         code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
1214         if (code) {
1215             lock_ReleaseWrite(&scp->rw);
1216             lock_ReleaseMutex(&tbp->mx);
1217             break;
1218         }
1219                 
1220         /* don't fetch over dirty buffers */
1221         if (tbp->flags & CM_BUF_DIRTY) {
1222             cm_SyncOpDone(scp, tbp, flags);
1223             lock_ReleaseWrite(&scp->rw);
1224             lock_ReleaseMutex(&tbp->mx);
1225             break;
1226         }
1227
1228         /* Release locks */
1229         lock_ReleaseWrite(&scp->rw);
1230         lock_ReleaseMutex(&tbp->mx);
1231
1232         /* add the buffer to the list */
1233         qdp = osi_QDAlloc();
1234         osi_SetQData(qdp, tbp);
1235         osi_QAddH((osi_queue_t **)&biop->bufListp, 
1236                   (osi_queue_t **)&biop->bufListEndp, 
1237                   &qdp->q);
1238         buf_Hold(tbp);
1239
1240         /* from now on, a failure just stops our collection process, but
1241          * we still do the I/O to whatever we've already managed to collect.
1242          */
1243         isFirst = 0;
1244         collected += cm_data.buf_blockSize;
1245     }
1246         
1247     /* now, we've held in biop->bufListp all the buffer's we're really
1248      * interested in.  We also have holds left from heldBufListp, and we
1249      * now release those holds on the buffers.
1250      */
1251     for (qdp = heldBufListp; qdp; qdp = tqdp) {
1252         tqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1253         tbp = osi_GetQData(qdp);
1254         osi_QRemoveHT((osi_queue_t **) &heldBufListp,
1255                       (osi_queue_t **) &heldBufListEndp,
1256                       &qdp->q);
1257         osi_QDFree(qdp);
1258         buf_Release(tbp);
1259         tbp = NULL;
1260     }
1261
1262     /* Caller expects this */
1263     lock_ObtainWrite(&scp->rw);
1264  
1265     /* if we got a failure setting up the first buffer, then we don't have
1266      * any side effects yet, and we also have failed an operation that the
1267      * caller requires to make any progress.  Give up now.
1268      */
1269     if (code && isFirst) {
1270         buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1271         return code;
1272     }
1273         
1274     /* otherwise, we're still OK, and should just return the I/O setup we've
1275      * got.
1276      */
1277     biop->length = collected;
1278     biop->reserved = reserving;
1279     return 0;
1280 }
1281
1282 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
1283  * cm_SetupStoreBIOD
1284  */
1285 void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
1286 {
1287     cm_scache_t *scp;           /* do not release; not held in biop */
1288     cm_buf_t *bufp;
1289     osi_queueData_t *qdp;
1290     osi_queueData_t *nqdp;
1291     int flags;
1292
1293     /* Give back reserved buffers */
1294     if (biop->reserved)
1295         buf_UnreserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
1296         
1297     if (isStore)
1298         flags = CM_SCACHESYNC_STOREDATA;
1299     else
1300         flags = CM_SCACHESYNC_FETCHDATA;
1301
1302     scp = biop->scp;
1303     if (biop->bufListp) {
1304         for(qdp = biop->bufListp; qdp; qdp = nqdp) {
1305             /* lookup next guy first, since we're going to free this one */
1306             nqdp = (osi_queueData_t *) osi_QNext(&qdp->q);
1307                 
1308             /* extract buffer and free queue data */
1309             bufp = osi_GetQData(qdp);
1310             osi_QRemoveHT((osi_queue_t **) &biop->bufListp,
1311                            (osi_queue_t **) &biop->bufListEndp,
1312                            &qdp->q);
1313             osi_QDFree(qdp);
1314
1315             /* now, mark I/O as done, unlock the buffer and release it */
1316             if (scp_locked)
1317                 lock_ReleaseWrite(&scp->rw);
1318             lock_ObtainMutex(&bufp->mx);
1319             lock_ObtainWrite(&scp->rw);
1320             cm_SyncOpDone(scp, bufp, flags);
1321                 
1322             /* turn off writing and wakeup users */
1323             if (isStore) {
1324                 if (bufp->flags & CM_BUF_WAITING) {
1325                     osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
1326                     osi_Wakeup((LONG_PTR) bufp);
1327                 }
1328                 if (code) {
1329                     bufp->flags &= ~CM_BUF_WRITING;
1330                     switch (code) {
1331                     case CM_ERROR_NOSUCHFILE:
1332                     case CM_ERROR_BADFD:
1333                     case CM_ERROR_NOACCESS:
1334                     case CM_ERROR_QUOTA:
1335                     case CM_ERROR_SPACE:
1336                     case CM_ERROR_TOOBIG:
1337                     case CM_ERROR_READONLY:
1338                     case CM_ERROR_NOSUCHPATH:
1339                         /*
1340                          * Apply the fatal error to this buffer.
1341                          */
1342                         bufp->flags &= ~CM_BUF_DIRTY;
1343                         bufp->flags |= CM_BUF_ERROR;
1344                         bufp->dirty_offset = 0;
1345                         bufp->dirty_length = 0;
1346                         bufp->error = code;
1347                         bufp->dataVersion = CM_BUF_VERSION_BAD;
1348                         bufp->dirtyCounter++;
1349                         break;
1350                     case CM_ERROR_TIMEDOUT:
1351                     case CM_ERROR_ALLDOWN:
1352                     case CM_ERROR_ALLBUSY:
1353                     case CM_ERROR_ALLOFFLINE:
1354                     case CM_ERROR_CLOCKSKEW:
1355                     default:
1356                         /* do not mark the buffer in error state but do
1357                         * not attempt to complete the rest either.
1358                         */
1359                         break;
1360                     }
1361                 } else {
1362                     bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
1363                     bufp->dirty_offset = bufp->dirty_length = 0;
1364                 }
1365             }
1366
1367             if (!scp_locked)
1368                 lock_ReleaseWrite(&scp->rw);
1369             lock_ReleaseMutex(&bufp->mx);
1370             buf_Release(bufp);
1371             bufp = NULL;
1372         }
1373     } else {
1374         if (!scp_locked)
1375             lock_ObtainWrite(&scp->rw);
1376         cm_SyncOpDone(scp, NULL, flags);
1377         if (!scp_locked)
1378             lock_ReleaseWrite(&scp->rw);
1379     }
1380
1381     /* clean things out */
1382     biop->bufListp = NULL;
1383     biop->bufListEndp = NULL;
1384 }   
1385
1386 /* Fetch a buffer.  Called with scp locked.
1387  * The scp is locked on return.
1388  */
1389 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
1390                   cm_req_t *reqp)
1391 {
1392     long code, code1;
1393     afs_uint32 nbytes;                  /* bytes in transfer */
1394     afs_uint32 nbytes_hi = 0;            /* high-order 32 bits of bytes in transfer */
1395     afs_uint64 length_found = 0;
1396     long rbytes;                        /* bytes in rx_Read call */
1397     long temp;
1398     AFSFetchStatus afsStatus;
1399     AFSCallBack callback;
1400     AFSVolSync volSync;
1401     char *bufferp;
1402     cm_buf_t *tbufp;                    /* buf we're filling */
1403     osi_queueData_t *qdp;               /* q element we're scanning */
1404     AFSFid tfid;
1405     struct rx_call *rxcallp;
1406     struct rx_connection *rxconnp;
1407     cm_bulkIO_t biod;           /* bulk IO descriptor */
1408     cm_conn_t *connp;
1409     int getroot;
1410     afs_int32 t1,t2;
1411     int require_64bit_ops = 0;
1412     int call_was_64bit = 0;
1413
1414     /* now, the buffer may or may not be filled with good data (buf_GetNew
1415      * drops lots of locks, and may indeed return a properly initialized
1416      * buffer, although more likely it will just return a new, empty, buffer.
1417      */
1418
1419 #ifdef AFS_FREELANCE_CLIENT
1420
1421     // yj: if they're trying to get the /afs directory, we need to
1422     // handle it differently, since it's local rather than on any
1423     // server
1424
1425     getroot = (scp==cm_data.rootSCachep);
1426     if (getroot)
1427         osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
1428 #endif
1429
1430     if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
1431         /* We already have this buffer don't do extra work */
1432         return 0;
1433     }
1434
1435     cm_AFSFidFromFid(&tfid, &scp->fid);
1436
1437     code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
1438     if (code) {
1439         /* couldn't even get the first page setup properly */
1440         osi_Log1(afsd_logp, "GetBuffer: SetupFetchBIOD failure code %d", code);
1441         return code;
1442     }
1443
1444     /* once we get here, we have the callback in place, we know that no one
1445      * is fetching the data now.  Check one last time that we still have
1446      * the wrong data, and then fetch it if we're still wrong.
1447      *
1448      * We can lose a race condition and end up with biod.length zero, in
1449      * which case we just retry.
1450      */
1451     if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
1452         if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) && 
1453              LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength)) 
1454         {
1455             osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
1456                      bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
1457
1458             if (bufp->dataVersion == CM_BUF_VERSION_BAD)
1459                 memset(bufp->datap, 0, cm_data.buf_blockSize);
1460             bufp->dataVersion = scp->dataVersion;
1461         }
1462         cm_ReleaseBIOD(&biod, 0, 0, 1);
1463         return 0;
1464     } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
1465                 && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
1466                 LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
1467         memset(bufp->datap, 0, cm_data.buf_blockSize);
1468         bufp->dataVersion = scp->dataVersion;
1469         cm_ReleaseBIOD(&biod, 0, 0, 1);
1470         return 0;
1471     }
1472
1473     lock_ReleaseWrite(&scp->rw);
1474
1475     if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
1476                                                 ConvertLongToLargeInteger(biod.length)),
1477                                 ConvertLongToLargeInteger(LONG_MAX))) {
1478         require_64bit_ops = 1;
1479     }
1480
1481     osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
1482     osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
1483              scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
1484
1485 #ifdef AFS_FREELANCE_CLIENT
1486
1487     // yj code
1488     // if getroot then we don't need to make any calls
1489     // just return fake data
1490         
1491     if (cm_freelanceEnabled && getroot) {
1492         // setup the fake status                        
1493         afsStatus.InterfaceVersion = 0x1;
1494         afsStatus.FileType = 0x2;
1495         afsStatus.LinkCount = scp->linkCount;
1496         afsStatus.Length = cm_fakeDirSize;
1497         afsStatus.DataVersion = (afs_uint32)(cm_data.fakeDirVersion & 0xFFFFFFFF);
1498         afsStatus.Author = 0x1;
1499         afsStatus.Owner = 0x0;
1500         afsStatus.CallerAccess = 0x9;
1501         afsStatus.AnonymousAccess = 0x9;
1502         afsStatus.UnixModeBits = 0x1ff;
1503         afsStatus.ParentVnode = 0x1;
1504         afsStatus.ParentUnique = 0x1;
1505         afsStatus.ResidencyMask = 0;
1506         afsStatus.ClientModTime = (afs_uint32)FakeFreelanceModTime;
1507         afsStatus.ServerModTime = (afs_uint32)FakeFreelanceModTime;
1508         afsStatus.Group = 0;
1509         afsStatus.SyncCounter = 0;
1510         afsStatus.dataVersionHigh = (afs_uint32)(cm_data.fakeDirVersion >> 32);
1511         afsStatus.lockCount = 0;
1512         afsStatus.Length_hi = 0;
1513         afsStatus.errorCode = 0;
1514         
1515         // once we're done setting up the status info,
1516         // we just fill the buffer pages with fakedata
1517         // from cm_FakeRootDir. Extra pages are set to
1518         // 0. 
1519                 
1520         lock_ObtainMutex(&cm_Freelance_Lock);
1521         t1 = bufp->offset.LowPart;
1522         qdp = biod.bufListEndp;
1523         while (qdp) {
1524             tbufp = osi_GetQData(qdp);
1525             bufferp=tbufp->datap;
1526             memset(bufferp, 0, cm_data.buf_blockSize);
1527             t2 = cm_fakeDirSize - t1;
1528             if (t2> (afs_int32)cm_data.buf_blockSize) 
1529                 t2=cm_data.buf_blockSize;
1530             if (t2 > 0) {
1531                 memcpy(bufferp, cm_FakeRootDir+t1, t2);
1532             } else {
1533                 t2 = 0;
1534             }
1535             t1+=t2;
1536             qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1537
1538         }
1539         lock_ReleaseMutex(&cm_Freelance_Lock);
1540         
1541         // once we're done, we skip over the part of the
1542         // code that does the ACTUAL fetching of data for
1543         // real files
1544
1545         goto fetchingcompleted;
1546     }
1547
1548 #endif /* AFS_FREELANCE_CLIENT */
1549
1550         /* now make the call */
1551     do {
1552         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
1553         if (code) 
1554             continue;
1555
1556         rxconnp = cm_GetRxConn(connp);
1557         rxcallp = rx_NewCall(rxconnp);
1558         rx_PutConnection(rxconnp);
1559
1560 #ifdef AFS_LARGEFILES
1561         nbytes = nbytes_hi = 0;
1562
1563         if (SERVERHAS64BIT(connp)) {
1564             call_was_64bit = 1;
1565
1566             osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
1567                      scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
1568
1569             code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
1570
1571             if (code == 0) {
1572                 temp = rx_Read32(rxcallp, &nbytes_hi);
1573                 if (temp == sizeof(afs_int32)) {
1574                     nbytes_hi = ntohl(nbytes_hi);
1575                 } else {
1576                     nbytes_hi = 0;
1577                     code = rxcallp->error;
1578                     code1 = rx_EndCall(rxcallp, code);
1579                     rxcallp = NULL;
1580                 }
1581             }
1582         } else {
1583             call_was_64bit = 0;
1584         }
1585
1586         if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
1587             if (require_64bit_ops) {
1588                 osi_Log0(afsd_logp, "Skipping FetchData.  Operation requires FetchData64");
1589                 code = CM_ERROR_TOOBIG;
1590             } else {
1591                 if (!rxcallp) {
1592                     rxconnp = cm_GetRxConn(connp);
1593                     rxcallp = rx_NewCall(rxconnp);
1594                     rx_PutConnection(rxconnp);
1595                 }
1596
1597                 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1598                          scp, biod.offset.LowPart, biod.length);
1599
1600                 code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1601                                             biod.length);
1602
1603                 SET_SERVERHASNO64BIT(connp);
1604             }
1605         }
1606
1607         if (code == 0) {
1608             temp  = rx_Read32(rxcallp, &nbytes);
1609             if (temp == sizeof(afs_int32)) {
1610                 nbytes = ntohl(nbytes);
1611                 FillInt64(length_found, nbytes_hi, nbytes);
1612                 if (length_found > biod.length) {
1613                     osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1614                     code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1615                 }
1616             } else {
1617                 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1618                 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1619             }
1620         }
1621         /* for the moment, nbytes_hi will always be 0 if code == 0
1622            because biod.length is a 32-bit quantity. */
1623 #else
1624         osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
1625                  scp, biod.offset.LowPart, biod.length);
1626
1627         code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
1628                                     biod.length);
1629
1630         /* now copy the data out of the pipe and put it in the buffer */
1631         if (code == 0) {
1632             temp  = rx_Read32(rxcallp, &nbytes);
1633             if (temp == sizeof(afs_int32)) {
1634                 nbytes = ntohl(nbytes);
1635                 if (nbytes > biod.length) {
1636                     osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
1637                     code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1638                 }
1639             }
1640             else {
1641                 osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
1642                 code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
1643             }
1644         }
1645 #endif
1646
1647         if (code == 0) {
1648             qdp = biod.bufListEndp;
1649             if (qdp) {
1650                 tbufp = osi_GetQData(qdp);
1651                 bufferp = tbufp->datap;
1652             }
1653             else 
1654                 bufferp = NULL;
1655             /* fill nbytes of data from the pipe into the pages.
1656              * When we stop, qdp will point at the last page we're
1657              * dealing with, and bufferp will tell us where we
1658              * stopped.  We'll need this info below when we clear
1659              * the remainder of the last page out (and potentially
1660              * clear later pages out, if we fetch past EOF).
1661              */
1662             while (nbytes > 0) {
1663                 /* assert that there are still more buffers;
1664                  * our check above for nbytes being less than
1665                  * biod.length should ensure this.
1666                  */
1667                 osi_assertx(bufferp != NULL, "null cm_buf_t");
1668
1669                 /* read rbytes of data */
1670                 rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
1671                 temp = rx_Read(rxcallp, bufferp, rbytes);
1672                 if (temp < rbytes) {
1673                     code = (rxcallp->error < 0) ? rxcallp->error : RX_EOF;
1674                     break;
1675                 }
1676
1677                 /* allow read-while-fetching.
1678                  * if this is the last buffer, clear the
1679                  * PREFETCHING flag, so the reader waiting for
1680                  * this buffer will start a prefetch.
1681                  */
1682                 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
1683                 lock_ObtainWrite(&scp->rw);
1684                 if (scp->flags & CM_SCACHEFLAG_WAITING) {
1685                     osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
1686                     osi_Wakeup((LONG_PTR) &scp->flags);
1687                 }
1688                 if (cpffp && !*cpffp && !osi_QPrev(&qdp->q)) {
1689                     osi_hyper_t tlength = ConvertLongToLargeInteger(biod.length);
1690                     *cpffp = 1;
1691                     cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
1692                 }
1693                 lock_ReleaseWrite(&scp->rw);
1694
1695                 /* and adjust counters */
1696                 nbytes -= temp;
1697
1698                 /* and move to the next buffer */
1699                 if (nbytes != 0) {
1700                     qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1701                     if (qdp) {
1702                         tbufp = osi_GetQData(qdp);
1703                         bufferp = tbufp->datap;
1704                     }
1705                     else 
1706                         bufferp = NULL;
1707                 } else 
1708                     bufferp += temp;
1709             }
1710
1711             /* zero out remainder of last pages, in case we are
1712              * fetching past EOF.  We were fetching an integral #
1713              * of pages, but stopped, potentially in the middle of
1714              * a page.  Zero the remainder of that page, and then
1715              * all of the rest of the pages.
1716              */
1717             /* bytes fetched */
1718             osi_assertx((bufferp - tbufp->datap) < LONG_MAX, "data >= LONG_MAX");
1719             rbytes = (long) (bufferp - tbufp->datap);
1720
1721             /* bytes left to zero */
1722             rbytes = cm_data.buf_blockSize - rbytes;
1723             while(qdp) {
1724                 if (rbytes != 0)
1725                     memset(bufferp, 0, rbytes);
1726                 qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
1727                 if (qdp == NULL) 
1728                     break;
1729                 tbufp = osi_GetQData(qdp);
1730                 bufferp = tbufp->datap;
1731                 /* bytes to clear in this page */
1732                 rbytes = cm_data.buf_blockSize;
1733             }   
1734         }
1735
1736         if (code == 0) {
1737             if (call_was_64bit)
1738                 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
1739             else
1740                 code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
1741         } else {
1742             if (call_was_64bit)
1743                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
1744             else
1745                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
1746         }
1747
1748         if (rxcallp)
1749             code1 = rx_EndCall(rxcallp, code);
1750
1751         if (code1 == RXKADUNKNOWNKEY)
1752             osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
1753
1754         /* Prefer the error value from FetchData over rx_EndCall */
1755         if (code == 0 && code1 != 0)
1756             code = code1;
1757         osi_Log0(afsd_logp, "CALL FetchData DONE");
1758
1759     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
1760
1761   fetchingcompleted:
1762     code = cm_MapRPCError(code, reqp);
1763
1764     lock_ObtainWrite(&scp->rw);
1765     
1766     /* we know that no one else has changed the buffer, since we still have
1767      * the fetching flag on the buffers, and we have the scp locked again.
1768      * Copy in the version # into the buffer if we got code 0 back from the
1769      * read.
1770      */
1771     if (code == 0) {
1772         for(qdp = biod.bufListp;
1773              qdp;
1774              qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
1775             tbufp = osi_GetQData(qdp);
1776             tbufp->dataVersion = afsStatus.dataVersionHigh;
1777             tbufp->dataVersion <<= 32;
1778             tbufp->dataVersion |= afsStatus.DataVersion;
1779
1780 #ifdef DISKCACHE95
1781             /* write buffer out to disk cache */
1782             diskcache_Update(tbufp->dcp, tbufp->datap, cm_data.buf_blockSize,
1783                               tbufp->dataVersion);
1784 #endif /* DISKCACHE95 */
1785         }
1786     }
1787
1788     /* release scatter/gather I/O structure (buffers, locks) */
1789     cm_ReleaseBIOD(&biod, 0, code, 1);
1790
1791     if (code == 0) 
1792         cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, 0);
1793     
1794     return code;
1795 }