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