death to register
[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
134 afs_TransitionToBypass(struct vcache *avc,
135                        afs_ucred_t *acred, int aflags)
136 {
137
138     afs_int32 code;
139     struct vrequest treq;
140     int setDesire = 0;
141     int setManual = 0;
142
143     if (!avc)
144         return;
145                 
146     if (avc->f.states & FCSBypass)
147         osi_Panic("afs_TransitionToBypass: illegal transition to bypass--already FCSBypass\n");         
148                 
149     if (aflags & TRANSChangeDesiredBit)
150         setDesire = 1;
151     if (aflags & TRANSSetManualBit)
152         setManual = 1;
153                 
154 #ifdef AFS_BOZONLOCK_ENV
155     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
156 #else
157     AFS_GLOCK();
158 #endif
159     ObtainWriteLock(&avc->lock, 925);
160         
161     /* If we never cached this, just change state */
162     if (setDesire && (!(avc->cachingStates & FCSBypass))) {
163         avc->f.states |= FCSBypass;
164         goto done;
165     }
166
167     /* cg2v, try to store any chunks not written 20071204 */
168     if (avc->execsOrWriters > 0) {
169         code = afs_InitReq(&treq, acred);
170         if (!code)
171             code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
172     }
173
174 #if 0
175     /* also cg2v, don't dequeue the callback */
176     ObtainWriteLock(&afs_xcbhash, 956); 
177     afs_DequeueCallback(avc);
178     ReleaseWriteLock(&afs_xcbhash);
179 #endif
180     avc->f.states &= ~(CStatd | CDirty);      /* next reference will re-stat */
181     /* now find the disk cache entries */
182     afs_TryToSmush(avc, acred, 1);
183     osi_dnlc_purgedp(avc);
184     if (avc->linkData && !(avc->f.states & CCore)) {
185         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
186         avc->linkData = NULL;
187     }           
188
189     avc->cachingStates |= FCSBypass;    /* Set the bypass flag */
190     if(setDesire)
191         avc->cachingStates |= FCSDesireBypass;
192     if(setManual)
193         avc->cachingStates |= FCSManuallySet;
194     avc->cachingTransitions++;
195
196 done:
197     ReleaseWriteLock(&avc->lock);
198 #ifdef AFS_BOZONLOCK_ENV
199     afs_BozonUnlock(&avc->pvnLock, avc);
200 #else
201     AFS_GUNLOCK();
202 #endif
203 }
204
205 /*
206  * This is almost exactly like the PFlush() routine in afs_pioctl.c,
207  * but that routine is static.  We are about to change a file from
208  * bypassing caching to normal caching.  Therefore, we want to
209  * throw out any existing VM pages for the file.  We keep track of
210  * the number of times we go back and forth from caching to bypass.
211  */
212 void
213 afs_TransitionToCaching(struct vcache *avc,
214                         afs_ucred_t *acred,
215                         int aflags)
216 {
217     int resetDesire = 0;
218     int setManual = 0;
219
220     if (!avc)
221         return;
222
223     if (!(avc->f.states & FCSBypass))
224         osi_Panic("afs_TransitionToCaching: illegal transition to caching--already caching\n");         
225                 
226     if (aflags & TRANSChangeDesiredBit)
227         resetDesire = 1;
228     if (aflags & TRANSSetManualBit)
229         setManual = 1;
230
231 #ifdef AFS_BOZONLOCK_ENV
232     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
233 #else
234     AFS_GLOCK();
235 #endif
236     ObtainWriteLock(&avc->lock, 926);
237
238     /* Ok, we actually do need to flush */
239     ObtainWriteLock(&afs_xcbhash, 957);
240     afs_DequeueCallback(avc);
241     avc->f.states &= ~(CStatd | CDirty);        /* next reference will re-stat cache entry */
242     ReleaseWriteLock(&afs_xcbhash);
243     /* now find the disk cache entries */
244     afs_TryToSmush(avc, acred, 1);
245     osi_dnlc_purgedp(avc);
246     if (avc->linkData && !(avc->f.states & CCore)) {
247         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
248         avc->linkData = NULL;
249     }
250                 
251     avc->cachingStates &= ~(FCSBypass);    /* Reset the bypass flag */
252     if (resetDesire)
253         avc->cachingStates &= ~(FCSDesireBypass);
254     if (setManual)
255         avc->cachingStates |= FCSManuallySet;
256     avc->cachingTransitions++;
257
258     ReleaseWriteLock(&avc->lock);
259 #ifdef AFS_BOZONLOCK_ENV
260     afs_BozonUnlock(&avc->pvnLock, avc);
261 #else
262     AFS_GUNLOCK();
263 #endif
264 }
265
266 /* In the case where there's an error in afs_NoCacheFetchProc or
267  * afs_PrefetchNoCache, all of the pages they've been passed need
268  * to be unlocked.
269  */
270 #if defined(AFS_LINUX24_ENV)
271 #define unlock_and_release_pages(auio) \
272     do { \
273         struct iovec *ciov;     \
274         struct page *pp; \
275         afs_int32 iovmax; \
276         afs_int32 iovno = 0; \
277         ciov = auio->uio_iov; \
278         iovmax = auio->uio_iovcnt - 1;  \
279         pp = (struct page*) ciov->iov_base;     \
280         afs_warn("BYPASS: Unlocking pages..."); \
281         while(1) { \
282             if(pp != NULL && PageLocked(pp)) \
283                 UnlockPage(pp); \
284             put_page(pp); /* decrement refcount */ \
285             iovno++; \
286             if(iovno > iovmax) \
287                 break; \
288             ciov = (auio->uio_iov + iovno);     \
289             pp = (struct page*) ciov->iov_base; \
290         } \
291         afs_warn("Pages Unlocked.\n"); \
292     } while(0)
293 #else
294 #ifdef UKERNEL
295 #define unlock_and_release_pages(auio) \
296          do { } while(0)
297 #else
298 #error AFS_CACHE_BYPASS not implemented on this platform
299 #endif
300 #endif
301
302 /* no-cache prefetch routine */
303 static afs_int32
304 afs_NoCacheFetchProc(struct rx_call *acall, 
305                      struct vcache *avc, 
306                                          uio_t *auio, 
307                      afs_int32 release_pages,
308                      afs_int32 size)
309 {
310     afs_int32 length;
311     afs_int32 code;
312     int tlen;
313     int moredata, iovno, iovoff, iovmax, clen, result, locked;
314     struct iovec *ciov;
315     struct page *pp;
316     char *address;
317 #ifdef AFS_KMAP_ATOMIC
318     char *page_buffer = osi_Alloc(PAGE_SIZE);
319 #else
320     char *page_buffer = NULL;
321 #endif
322
323     ciov = auio->uio_iov;
324     pp = (struct page*) ciov->iov_base;
325     iovmax = auio->uio_iovcnt - 1;
326     iovno = iovoff = result = 0;        
327     do {
328
329         COND_GUNLOCK(locked);
330         code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
331         COND_RE_GLOCK(locked);
332
333         if (code != sizeof(afs_int32)) {
334             result = 0;
335             afs_warn("Preread error. code: %d instead of %d\n",
336                 code, (int)sizeof(afs_int32));
337             unlock_and_release_pages(auio);
338             goto done;
339         } else
340             length = ntohl(length);             
341
342         if (length > size) {
343             result = EIO;
344             afs_warn("Preread error. Got length %d, which is greater than size %d\n",
345                      length, size);
346             unlock_and_release_pages(auio);
347             goto done;
348         }
349                                         
350         /*
351          * The fetch protocol is extended for the AFS/DFS translator
352          * to allow multiple blocks of data, each with its own length,
353          * to be returned. As long as the top bit is set, there are more
354          * blocks expected.
355          *
356          * We do not do this for AFS file servers because they sometimes
357          * return large negative numbers as the transfer size.
358          */
359         if (avc->f.states & CForeign) {
360             moredata = length & 0x80000000;
361             length &= ~0x80000000;
362         } else {
363             moredata = 0;
364         }
365                                 
366         while (length > 0) {
367
368             clen = ciov->iov_len - iovoff; 
369             tlen = afs_min(length, clen);
370 #ifdef AFS_LINUX24_ENV
371 #ifndef AFS_KMAP_ATOMIC
372             if(pp)
373                 address = kmap(pp);
374             else { 
375                 /* rx doesn't provide an interface to simply advance
376                    or consume n bytes.  for now, allocate a PAGE_SIZE 
377                    region of memory to receive bytes in the case that 
378                    there were holes in readpages */
379                 if(page_buffer == NULL)
380                     page_buffer = osi_Alloc(PAGE_SIZE);
381                     address = page_buffer;
382                 }
383 #else
384             address = page_buffer;
385 #endif
386 #else
387 #ifndef UKERNEL
388 #error AFS_CACHE_BYPASS not implemented on this platform
389 #endif
390 #endif /* LINUX24 */
391             COND_GUNLOCK(locked);
392             code = rx_Read(acall, address, tlen);
393             COND_RE_GLOCK(locked);
394                 
395             if (code < 0) {
396                 afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", code);
397                 result = 0;
398                 unlock_and_release_pages(auio);
399                 goto done;
400             } else if (code == 0) {
401                 result = 0;
402                 afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
403                 unlock_and_release_pages(auio);
404                 goto done;
405             }
406             length -= code;
407             tlen -= code;
408                 
409             if(tlen > 0) {
410                 iovoff += code;
411                 address += code;
412             } else {
413 #ifdef AFS_LINUX24_ENV
414 #ifdef AFS_KMAP_ATOMIC
415                 if(pp) {
416                     address = kmap_atomic(pp, KM_USER0);
417                     memcpy(address, page_buffer, PAGE_SIZE);
418                     kunmap_atomic(address, KM_USER0);
419                 }
420 #endif
421 #else
422 #ifndef UKERNEL
423 #error AFS_CACHE_BYPASS not implemented on this platform
424 #endif
425 #endif /* LINUX 24 */
426                 /* we filled a page, conditionally release it */
427                 if (release_pages && ciov->iov_base) {
428                     /* this is appropriate when no caller intends to unlock
429                      * and release the page */
430 #ifdef AFS_LINUX24_ENV
431                     SetPageUptodate(pp);
432                     if(PageLocked(pp))
433                         UnlockPage(pp);
434                     else
435                         afs_warn("afs_NoCacheFetchProc: page not locked at iovno %d!\n", iovno);
436                     put_page(pp); /* decrement refcount */
437 #ifndef AFS_KMAP_ATOMIC
438                     kunmap(pp);
439 #endif
440 #else
441 #ifndef UKERNEL
442 #error AFS_CACHE_BYPASS not implemented on this platform
443 #endif
444 #endif /* LINUX24 */
445                 }
446                 /* and carry uio_iov */
447                 iovno++;
448                 if (iovno > iovmax)
449                     goto done;
450                         
451                 ciov = (auio->uio_iov + iovno);
452                 pp = (struct page*) ciov->iov_base;
453                 iovoff = 0;
454             }
455         }
456     } while (moredata);
457         
458 done:
459     if(page_buffer)
460         osi_Free(page_buffer, PAGE_SIZE);
461     return result;
462 }
463
464
465 /* dispatch a no-cache read request */
466 afs_int32
467 afs_ReadNoCache(struct vcache *avc, 
468                 struct nocache_read_request *bparms,
469                 afs_ucred_t *acred)
470 {
471     afs_int32 code;
472     afs_int32 bcnt;
473     struct brequest *breq;
474     struct vrequest *areq;
475                 
476     /* the reciever will free this */
477     areq = osi_Alloc(sizeof(struct vrequest));
478         
479     if (avc && avc->vc_error) {
480         code = EIO;
481         afs_warn("afs_ReadNoCache VCache Error!\n");
482         goto cleanup;
483     }
484     if ((code = afs_InitReq(areq, acred))) {
485         afs_warn("afs_ReadNoCache afs_InitReq error!\n");
486         goto cleanup;
487     }
488
489     AFS_GLOCK();                
490     code = afs_VerifyVCache(avc, areq);
491     AFS_GUNLOCK();
492         
493     if (code) {
494         code = afs_CheckCode(code, areq, 11);   /* failed to get it */
495         afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
496         goto cleanup;
497     }
498         
499     bparms->areq = areq;
500         
501     /* and queue this one */
502     bcnt = 1;
503     AFS_GLOCK();
504     while(bcnt < 20) {
505         breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1,
506                           bparms, (void *)0, (void *)0);
507         if(breq != 0) {
508             code = 0;
509             break;
510         }
511         afs_osi_Wait(10 * bcnt, 0, 0);
512     }
513     AFS_GUNLOCK();
514     
515     if(!breq) {
516         code = EBUSY;
517         goto cleanup;
518     }
519
520     return code;
521
522 cleanup:
523     /* If there's a problem before we queue the request, we need to
524      * do everything that would normally happen when the request was
525      * processed, like unlocking the pages and freeing memory.
526      */
527 #ifdef AFS_LINUX24_ENV
528     unlock_and_release_pages(bparms->auio);
529 #else
530 #ifndef UKERNEL
531 #error AFS_CACHE_BYPASS not implemented on this platform
532 #endif
533 #endif
534     osi_Free(areq, sizeof(struct vrequest));
535     osi_Free(bparms->auio->uio_iov,
536              bparms->auio->uio_iovcnt * sizeof(struct iovec));
537     osi_Free(bparms->auio, sizeof(uio_t));
538     osi_Free(bparms, sizeof(struct nocache_read_request));
539     return code;
540 }
541
542
543 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
544 afs_int32
545 afs_PrefetchNoCache(struct vcache *avc, 
546                     afs_ucred_t *acred,
547                     struct nocache_read_request *bparms)
548 {
549     uio_t *auio;
550     struct iovec *iovecp;
551     struct vrequest *areq;
552     afs_int32 code = 0;
553 #ifdef AFS_64BIT_CLIENT
554     afs_int32 length_hi, bytes, locked;
555 #endif
556         
557     struct afs_conn *tc;
558     afs_int32 i;
559     struct rx_call *tcall;
560     struct tlocal1 {
561         struct AFSVolSync tsync;
562         struct AFSFetchStatus OutStatus;
563         struct AFSCallBack CallBack;
564     };
565     struct tlocal1 *tcallspec;  
566                         
567     auio = bparms->auio;
568     areq = bparms->areq;
569     iovecp = auio->uio_iov;     
570         
571     tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
572     do {
573         tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
574         if (tc) {
575             avc->callback = tc->srvr->server;
576             i = osi_Time();
577             tcall = rx_NewCall(tc->id);
578 #ifdef AFS_64BIT_CLIENT
579             if (!afs_serverHasNo64Bit(tc)) {
580                 code = StartRXAFS_FetchData64(tcall,
581                                               (struct AFSFid *) &avc->f.fid.Fid,
582                                               auio->uio_offset,
583                                               bparms->length);
584                 if (code == 0) {
585                     COND_GUNLOCK(locked);
586                     bytes = rx_Read(tcall, (char *)&length_hi,
587                                     sizeof(afs_int32));
588                     COND_RE_GLOCK(locked);
589                                         
590                     if (bytes != sizeof(afs_int32)) {
591                         length_hi = 0;
592                         code = rx_Error(tcall);
593                         COND_GUNLOCK(locked);
594                         code = rx_EndCall(tcall, code);
595                         COND_RE_GLOCK(locked);
596                         tcall = NULL;
597                     }
598                 }
599             } /* afs_serverHasNo64Bit */
600             if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
601                 if (auio->uio_offset > 0x7FFFFFFF) {
602                     code = EFBIG;
603                 } else {
604                     afs_int32 pos;
605                     pos = auio->uio_offset;
606                     COND_GUNLOCK(locked);
607                     if (!tcall)
608                         tcall = rx_NewCall(tc->id);
609                     code = StartRXAFS_FetchData(tcall,
610                                         (struct AFSFid *) &avc->f.fid.Fid,
611                                         pos, bparms->length);
612                     COND_RE_GLOCK(locked);
613                 }
614                 afs_serverSetNo64Bit(tc);
615             }
616 #else
617             code = StartRXAFS_FetchData(tcall,
618                                         (struct AFSFid *) &avc->f.fid.Fid,
619                                         auio->uio_offset, bparms->length);
620 #endif
621             if (code == 0) {
622                 code = afs_NoCacheFetchProc(tcall, avc, auio,
623                                             1 /* release_pages */,
624                                             bparms->length);
625             } else {
626                 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
627                 unlock_and_release_pages(auio);
628                 goto done;
629             }
630             if (code == 0) {
631                 code = EndRXAFS_FetchData(tcall, &tcallspec->OutStatus,
632                                           &tcallspec->CallBack,
633                                           &tcallspec->tsync);
634             } else {
635                 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
636             }
637             code = rx_EndCall(tcall, code);
638         } else {
639             afs_warn("BYPASS: No connection.\n");
640             code = -1;
641 #ifdef AFS_LINUX24_ENV
642             unlock_and_release_pages(auio);
643 #else
644 #ifndef UKERNEL
645 #error AFS_CACHE_BYPASS not implemented on this platform
646 #endif
647 #endif
648             goto done;
649         }
650     } while (afs_Analyze(tc, code, &avc->f.fid, areq,
651                                                  AFS_STATS_FS_RPCIDX_FETCHDATA,
652                                                  SHARED_LOCK,0));
653 done:
654     /*
655      * Copy appropriate fields into vcache
656      */
657
658     afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
659
660     osi_Free(areq, sizeof(struct vrequest));
661     osi_Free(tcallspec, sizeof(struct tlocal1));
662     osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));  
663     osi_Free(bparms, sizeof(struct nocache_read_request));
664     osi_Free(auio, sizeof(uio_t));
665     return code;
666 }
667
668 #endif /* AFS_CACHE_BYPASS */