Limited the scope of some local variables in afs_CacheStoreProc
[openafs.git] / src / afs / afs_bypasscache.c
1 /*
2  * COPYRIGHT  ©  2000
3  * THE REGENTS OF THE UNIVERSITY OF MICHIGAN
4  * ALL RIGHTS RESERVED
5  * 
6  * Permission is granted to use, copy, create derivative works
7  * and redistribute this software and such derivative works
8  * for any purpose, so long as the name of The University of
9  * Michigan is not used in any advertising or publicity
10  * pertaining to the use of distribution of this software
11  * without specific, written prior authorization.  If the
12  * above copyright notice or any other identification of the
13  * University of Michigan is included in any copy of any
14  * portion of this software, then the disclaimer below must
15  * also be included.
16  * 
17  * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18  * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19  * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY O 
20  * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21  * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23  * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25  * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26  * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27  * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGES.
29  */
30  
31  /*
32  * Portions Copyright (c) 2008
33  * The Linux Box Corporation
34  * ALL RIGHTS RESERVED
35  *
36  * Permission is granted to use, copy, create derivative works
37  * and redistribute this software and such derivative works
38  * for any purpose, so long as the name of the Linux Box
39  * Corporation is not used in any advertising or publicity
40  * pertaining to the use or distribution of this software
41  * without specific, written prior authorization.  If the
42  * above copyright notice or any other identification of the
43  * Linux Box Corporation is included in any copy of any
44  * portion of this software, then the disclaimer below must
45  * also be included.
46  *
47  * This software is provided as is, without representation
48  * from the Linux Box Corporation as to its fitness for any
49  * purpose, and without warranty by the Linux Box Corporation
50  * of any kind, either express or implied, including
51  * without limitation the implied warranties of
52  * merchantability and fitness for a particular purpose.  The
53  * Linux Box Corporation shall not be liable for any damages, 
54  * including special, indirect, incidental, or consequential 
55  * damages, with respect to any claim arising out of or in 
56  * connection with the use of the software, even if it has been 
57  * or is hereafter advised of the possibility of such damages.
58  */
59
60
61 #include <afsconfig.h>
62 #include "afs/param.h"
63
64 #if defined(AFS_CACHE_BYPASS)
65
66 #include "afs/afs_bypasscache.h"
67
68 /*
69  * afs_bypasscache.c
70  *
71  */
72 #include "afs/sysincludes.h" /* Standard vendor system headers */
73 #include "afs/afsincludes.h" /* Afs-based standard headers */
74 #include "afs/afs_stats.h"   /* statistics */
75 #include "afs/nfsclient.h"  
76 #include "rx/rx_globals.h"
77
78 #if defined(AFS_LINUX26_ENV)
79 #define LockPage(pp) lock_page(pp)
80 #define UnlockPage(pp) unlock_page(pp)
81 #endif
82 #define AFS_KMAP_ATOMIC
83
84 #ifndef afs_min
85 #define afs_min(A,B) ((A)<(B)) ? (A) : (B)
86 #endif
87
88 /* conditional GLOCK macros */
89 #define COND_GLOCK(var) \
90         do { \
91                 var = ISAFS_GLOCK(); \
92                 if(!var) \
93                         RX_AFS_GLOCK(); \
94         } while(0);
95         
96 #define COND_RE_GUNLOCK(var) \
97         do { \
98                 if(var) \
99                         RX_AFS_GUNLOCK(); \
100         } while(0);
101
102
103 /* conditional GUNLOCK macros */
104
105 #define COND_GUNLOCK(var) \
106         do {    \
107                 var = ISAFS_GLOCK(); \
108                 if(var) \
109                         RX_AFS_GUNLOCK(); \
110         } while(0);
111
112 #define COND_RE_GLOCK(var) \
113         do { \
114                 if(var) \
115                         RX_AFS_GLOCK(); \
116         } while(0);
117
118
119 int cache_bypass_strategy       =       NEVER_BYPASS_CACHE;
120 int cache_bypass_threshold  =   AFS_CACHE_BYPASS_DISABLED; /* file size > threshold triggers bypass */
121 int cache_bypass_prefetch = 1;  /* Should we do prefetching ? */
122
123 extern afs_rwlock_t afs_xcbhash;
124
125 /*
126  * This is almost exactly like the PFlush() routine in afs_pioctl.c,
127  * but that routine is static.  We are about to change a file from
128  * normal caching to bypass it's caching.  Therefore, we want to
129  * free up any cache space in use by the file, and throw out any
130  * existing VM pages for the file.  We keep track of the number of
131  * times we go back and forth from caching to bypass.
132  */
133 void afs_TransitionToBypass(register struct vcache *avc, register struct AFS_UCRED *acred, int aflags)
134 {
135
136     afs_int32 code;
137     struct vrequest treq;
138     int setDesire = 0;
139     int setManual = 0;
140
141     if(!avc)
142         return;
143                 
144     if(avc->f.states & FCSBypass)
145         osi_Panic("afs_TransitionToBypass: illegal transition to bypass--already FCSBypass\n");         
146                 
147     if(aflags & TRANSChangeDesiredBit)
148         setDesire = 1;
149     if(aflags & TRANSSetManualBit)
150         setManual = 1;
151                 
152 #ifdef AFS_BOZONLOCK_ENV
153     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
154 #else
155         AFS_GLOCK();    
156 #endif
157     ObtainWriteLock(&avc->lock, 925);
158         
159     /* If we never cached this, just change state */
160     if(setDesire && (!avc->cachingStates & FCSBypass)) {
161         avc->f.states |= FCSBypass;
162         goto done;
163     }
164         /* cg2v, try to store any chunks not written 20071204 */
165         if (avc->execsOrWriters > 0) { 
166                 code = afs_InitReq(&treq, acred);
167                 if(!code)
168                         code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE); 
169         }
170 #if 0
171         /* also cg2v, don't dequeue the callback */
172     ObtainWriteLock(&afs_xcbhash, 956); 
173     afs_DequeueCallback(avc);
174         ReleaseWriteLock(&afs_xcbhash);
175 #endif  
176     avc->f.states &= ~(CStatd | CDirty);        /* next reference will re-stat cache entry */
177     /* now find the disk cache entries */
178     afs_TryToSmush(avc, acred, 1);
179     osi_dnlc_purgedp(avc);
180     if (avc->linkData && !(avc->f.states & CCore)) {
181         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
182         avc->linkData = NULL;
183     }           
184         
185     avc->cachingStates |= FCSBypass;    /* Set the bypass flag */
186     if(setDesire)
187         avc->cachingStates |= FCSDesireBypass;
188     if(setManual)
189         avc->cachingStates |= FCSManuallySet;
190     avc->cachingTransitions++;
191
192 done:           
193     ReleaseWriteLock(&avc->lock);
194 #ifdef AFS_BOZONLOCK_ENV
195     afs_BozonUnlock(&avc->pvnLock, avc);
196 #else
197         AFS_GUNLOCK();
198 #endif
199 }
200
201 /*
202  * This is almost exactly like the PFlush() routine in afs_pioctl.c,
203  * but that routine is static.  We are about to change a file from
204  * bypassing caching to normal caching.  Therefore, we want to
205  * throw out any existing VM pages for the file.  We keep track of
206  * the number of times we go back and forth from caching to bypass.
207  */
208 void afs_TransitionToCaching(register struct vcache *avc, register struct AFS_UCRED *acred, int aflags)
209 {
210     int resetDesire = 0;
211     int setManual = 0;
212
213     if(!avc)
214         return;
215                 
216     if(!avc->f.states & FCSBypass)
217         osi_Panic("afs_TransitionToCaching: illegal transition to caching--already caching\n");         
218                 
219     if(aflags & TRANSChangeDesiredBit)
220         resetDesire = 1;
221     if(aflags & TRANSSetManualBit)
222         setManual = 1;
223
224 #ifdef AFS_BOZONLOCK_ENV
225     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
226 #else
227         AFS_GLOCK();    
228 #endif
229     ObtainWriteLock(&avc->lock, 926);
230         
231     /* Ok, we actually do need to flush */
232     ObtainWriteLock(&afs_xcbhash, 957);
233     afs_DequeueCallback(avc);
234     avc->f.states &= ~(CStatd | CDirty);        /* next reference will re-stat cache entry */
235     ReleaseWriteLock(&afs_xcbhash);
236     /* now find the disk cache entries */
237     afs_TryToSmush(avc, acred, 1);
238     osi_dnlc_purgedp(avc);
239     if (avc->linkData && !(avc->f.states & CCore)) {
240         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
241         avc->linkData = NULL;
242     }
243                 
244     avc->cachingStates &= ~(FCSBypass);    /* Reset the bypass flag */
245     if (resetDesire)
246         avc->cachingStates &= ~(FCSDesireBypass);
247     if (setManual)
248         avc->cachingStates |= FCSManuallySet;
249     avc->cachingTransitions++;
250
251     ReleaseWriteLock(&avc->lock);
252 #ifdef AFS_BOZONLOCK_ENV
253     afs_BozonUnlock(&avc->pvnLock, avc);
254 #else
255         AFS_GUNLOCK();
256 #endif
257 }
258
259 /* In the case where there's an error in afs_NoCacheFetchProc or
260  * afs_PrefetchNoCache, all of the pages they've been passed need
261  * to be unlocked.
262  */
263 #if defined(AFS_LINUX24_ENV)
264 #define unlock_pages(auio) \
265     do { \
266         struct iovec *ciov;     \
267         struct page *pp; \
268         afs_int32 iovmax; \
269         afs_int32 iovno = 0; \
270         ciov = auio->uio_iov; \
271         iovmax = auio->uio_iovcnt - 1;  \
272         pp = (struct page*) ciov->iov_base;     \
273         afs_warn("BYPASS: Unlocking pages..."); \
274         while(1) { \
275             if(pp != NULL && PageLocked(pp)) \
276                 UnlockPage(pp); \
277             iovno++; \
278             if(iovno > iovmax) \
279                 break; \
280             ciov = (auio->uio_iov + iovno);     \
281             pp = (struct page*) ciov->iov_base; \
282         } \
283         afs_warn("Pages Unlocked.\n"); \
284     } while(0);
285 #else
286 #ifdef UKERNEL
287 #define unlock_pages(auio) \
288          do { } while(0);
289 #else
290 #error AFS_CACHE_BYPASS not implemented on this platform
291 #endif
292 #endif
293
294 /* no-cache prefetch routine */
295 static afs_int32
296 afs_NoCacheFetchProc(register struct rx_call *acall, 
297                      register struct vcache *avc, 
298                                          register uio_t *auio, 
299                      afs_int32 release_pages)
300 {
301     afs_int32 length;
302     afs_int32 code;
303     int tlen;
304     int moredata, iovno, iovoff, iovmax, clen, result, locked;
305     struct iovec *ciov;
306     struct page *pp;
307     char *address;
308 #ifdef AFS_KMAP_ATOMIC
309     char *page_buffer = osi_Alloc(PAGE_SIZE);
310 #else
311     char *page_buffer = NULL;
312 #endif
313
314     ciov = auio->uio_iov;
315     pp = (struct page*) ciov->iov_base;
316     iovmax = auio->uio_iovcnt - 1;
317     iovno = iovoff = result = 0;        
318     do {
319
320         COND_GUNLOCK(locked);
321         code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
322         COND_RE_GLOCK(locked);
323
324         if (code != sizeof(afs_int32)) {
325             result = 0;
326             afs_warn("Preread error. code: %d instead of %d\n",
327                 code, sizeof(afs_int32));
328             unlock_pages(auio);
329             goto done;
330         } else
331             length = ntohl(length);             
332                                         
333         /*
334          * The fetch protocol is extended for the AFS/DFS translator
335          * to allow multiple blocks of data, each with its own length,
336          * to be returned. As long as the top bit is set, there are more
337          * blocks expected.
338          *
339          * We do not do this for AFS file servers because they sometimes
340          * return large negative numbers as the transfer size.
341          */
342         if (avc->f.states & CForeign) {
343             moredata = length & 0x80000000;
344             length &= ~0x80000000;
345         } else {
346             moredata = 0;
347         }
348                                 
349         while (length > 0) {
350
351             clen = ciov->iov_len - iovoff; 
352             tlen = afs_min(length, clen);
353 #ifdef AFS_LINUX24_ENV
354 #ifndef AFS_KMAP_ATOMIC
355             if(pp)
356                 address = kmap(pp);
357             else { 
358                 /* rx doesn't provide an interface to simply advance
359                    or consume n bytes.  for now, allocate a PAGE_SIZE 
360                    region of memory to receive bytes in the case that 
361                    there were holes in readpages */
362                 if(page_buffer == NULL)
363                     page_buffer = osi_Alloc(PAGE_SIZE);
364                     address = page_buffer;
365                 }
366 #else
367             address = page_buffer;
368 #endif
369 #else
370 #ifndef UKERNEL
371 #error AFS_CACHE_BYPASS not implemented on this platform
372 #endif
373 #endif /* LINUX24 */
374             COND_GUNLOCK(locked);
375             code = rx_Read(acall, address, tlen);
376             COND_RE_GLOCK(locked);
377                 
378             if (code < 0) {
379                 afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", code);
380                 result = 0;
381                 unlock_pages(auio);
382                 goto done;
383             } else if (code == 0) {
384                 result = 0;
385                 afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
386                 unlock_pages(auio);
387                 goto done;
388             }
389             length -= code;
390             tlen -= code;
391                 
392             if(tlen > 0) {
393                 iovoff += code;
394                 address += code;
395                         
396             } else {
397 #ifdef AFS_LINUX24_ENV
398 #ifdef AFS_KMAP_ATOMIC
399                 if(pp) {
400                     address = kmap_atomic(pp, KM_USER0);
401                     memcpy(address, page_buffer, PAGE_SIZE);
402                     kunmap_atomic(address, KM_USER0);
403                 }
404 #endif
405 #else
406 #ifndef UKERNEL
407 #error AFS_CACHE_BYPASS not implemented on this platform
408 #endif
409 #endif /* LINUX 24 */
410                         /* we filled a page, conditionally release it */
411                         if(release_pages && ciov->iov_base) {
412                                 /* this is appropriate when no caller intends to unlock
413                                  * and release the page */
414 #ifdef AFS_LINUX24_ENV
415                                 SetPageUptodate(pp);
416                                 if(PageLocked(pp))
417                                         UnlockPage(pp); 
418                                 else
419                                         afs_warn("afs_NoCacheFetchProc: page not locked at iovno %d!\n", iovno);
420                                 
421 #ifndef AFS_KMAP_ATOMIC
422                                 kunmap(pp);             
423 #endif
424 #else
425 #ifndef UKERNEL
426 #error AFS_CACHE_BYPASS not implemented on this platform
427 #endif
428 #endif /* LINUX24 */
429                         }
430                         /* and carry uio_iov */
431                         iovno++;
432                         if(iovno > iovmax) goto done;
433                         
434                         ciov = (auio->uio_iov + iovno);
435                         pp = (struct page*) ciov->iov_base;
436                         iovoff = 0;
437             }
438         }
439     } while (moredata);
440         
441 done:
442     if(page_buffer)
443         osi_Free(page_buffer, PAGE_SIZE);
444     return result;
445 }
446
447
448 /* dispatch a no-cache read request */
449 afs_int32
450 afs_ReadNoCache(register struct vcache *avc, 
451                                 register struct nocache_read_request *bparms, 
452                                 struct AFS_UCRED *acred)
453 {
454     afs_int32 code;
455     afs_int32 bcnt;
456     struct brequest *breq;
457     struct vrequest *areq;
458                 
459     /* the reciever will free this */
460     areq = osi_Alloc(sizeof(struct vrequest));
461         
462     if (avc && avc->vc_error) {
463                 code = EIO;
464                 afs_warn("afs_ReadNoCache VCache Error!\n");
465                 goto cleanup;
466     }
467     if ((code = afs_InitReq(areq, acred))) {
468                 afs_warn("afs_ReadNoCache afs_InitReq error!\n");
469                 goto cleanup;
470     }
471
472     AFS_GLOCK();                
473     code = afs_VerifyVCache(avc, areq);
474     AFS_GUNLOCK();
475         
476     if (code) {
477                 code = afs_CheckCode(code, areq, 11);   /* failed to get it */
478                 afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
479                 goto cleanup;
480     }
481         
482     bparms->areq = areq;
483         
484     /* and queue this one */
485     bcnt = 1;
486     AFS_GLOCK();
487     while(bcnt < 20) {
488         breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1, bparms);
489                 if(breq != 0) {
490                         code = 0;
491                         break;
492                 }       
493                 afs_osi_Wait(10 * bcnt, 0, 0);
494     }
495     AFS_GUNLOCK();
496     
497     if(!breq) {
498         code = EBUSY;
499                 goto cleanup;
500     }
501
502     return code;
503
504 cleanup:
505     /* If there's a problem before we queue the request, we need to
506      * do everything that would normally happen when the request was
507      * processed, like unlocking the pages and freeing memory.
508      */
509 #ifdef AFS_LINUX24_ENV
510     unlock_pages(bparms->auio);
511 #else
512 #ifndef UKERNEL
513 #error AFS_CACHE_BYPASS not implemented on this platform
514 #endif
515 #endif
516     osi_Free(areq, sizeof(struct vrequest));
517     osi_Free(bparms->auio->uio_iov, bparms->auio->uio_iovcnt * sizeof(struct iovec));   
518     osi_Free(bparms->auio, sizeof(uio_t));
519     osi_Free(bparms, sizeof(struct nocache_read_request));
520     return code;
521
522 }
523
524
525 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
526 afs_int32
527 afs_PrefetchNoCache(register struct vcache *avc, 
528                                         register struct AFS_UCRED *acred,
529                                         register struct nocache_read_request *bparms)
530 {
531     uio_t *auio;
532     struct iovec *iovecp;
533     struct vrequest *areq;
534     afs_int32 code, length_hi, bytes, locked;    
535         
536     register struct afs_conn *tc;
537     afs_int32 i;
538     struct rx_call *tcall;
539     struct tlocal1 {
540                 struct AFSVolSync tsync;
541                 struct AFSFetchStatus OutStatus;
542                 struct AFSCallBack CallBack;
543     };
544     struct tlocal1 *tcallspec;  
545                         
546     auio = bparms->auio;
547     areq = bparms->areq;
548     iovecp = auio->uio_iov;     
549         
550     tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
551     do {
552                 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
553                 if (tc) {
554                         avc->callback = tc->srvr->server;
555                         i = osi_Time();
556                         tcall = rx_NewCall(tc->id);
557 #ifdef AFS_64BIT_CLIENT
558                         if(!afs_serverHasNo64Bit(tc)) {
559                                 code = StartRXAFS_FetchData64(tcall,
560                                                                                           (struct AFSFid *) &avc->f.fid.Fid,
561                                                                                           auio->uio_offset, 
562                                                                                           bparms->length);
563                                 if (code == 0) {
564                                         
565                                         COND_GUNLOCK(locked);
566                                         bytes = rx_Read(tcall, (char *)&length_hi, sizeof(afs_int32));
567                                         COND_RE_GLOCK(locked);
568                                         
569                                         if (bytes != sizeof(afs_int32)) {
570                                                 length_hi = 0;
571                                                 code = rx_Error(tcall);
572                                                 COND_GUNLOCK(locked);
573                                                 code = rx_EndCall(tcall, code);
574                                                 COND_RE_GLOCK(locked);
575                                                 tcall = (struct rx_call *)0;
576                                         }
577                                 }                                             
578                                 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
579                                         if (auio->uio_offset > 0x7FFFFFFF) {
580                                                 code = EFBIG;
581                                         } else {
582                                                 afs_int32 pos;
583                                                 pos = auio->uio_offset;
584                                                 COND_GUNLOCK(locked);
585                                                 if (!tcall)
586                                                         tcall = rx_NewCall(tc->id);                                             
587                                                 code = StartRXAFS_FetchData(tcall,
588                                                                                                         (struct AFSFid *) &avc->f.fid.Fid,
589                                                                                                         pos, bparms->length);
590                                                 COND_RE_GLOCK(locked);
591                                         }
592                                         afs_serverSetNo64Bit(tc);
593                                 }
594                         } /* afs_serverHasNo64Bit */
595 #else
596                         code = StartRXAFS_FetchData(tcall,
597                                                                                 (struct AFSFid *) &avc->f.fid.Fid,
598                                                                                 auio->uio_offset, bparms->length);
599 #endif
600
601                         if (code == 0) {
602                                 code = afs_NoCacheFetchProc(tcall, avc, auio, 
603                                                                                         1 /* release_pages */);
604                         } else {
605                                 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
606                                 unlock_pages(auio);
607                                 goto done;
608                         }
609                         if (code == 0) {
610                                 code = EndRXAFS_FetchData(tcall,
611                                                                                   &tcallspec->OutStatus,
612                                                                                   &tcallspec->CallBack,
613                                                                                   &tcallspec->tsync);
614                         } else {
615                                 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
616                         }
617                         code = rx_EndCall(tcall, code);
618                 }
619                 else {
620                         afs_warn("BYPASS: No connection.\n");
621                         code = -1;
622 #ifdef AFS_LINUX24_ENV
623                         unlock_pages(auio);
624 #else
625 #ifndef UKERNEL
626 #error AFS_CACHE_BYPASS not implemented on this platform
627 #endif
628 #endif
629                         goto done;
630                 }
631     } while (afs_Analyze(tc, code, &avc->f.fid, areq,
632                                                  AFS_STATS_FS_RPCIDX_FETCHDATA,
633                                                  SHARED_LOCK,0));
634 done:
635     /*
636      * Copy appropriate fields into vcache
637      */
638
639     afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
640
641     osi_Free(areq, sizeof(struct vrequest));
642     osi_Free(tcallspec, sizeof(struct tlocal1));
643     osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));  
644     osi_Free(bparms, sizeof(struct nocache_read_request));
645     osi_Free(auio, sizeof(uio_t));
646     return code;
647 }
648
649 #endif /* AFS_CACHE_BYPASS */