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