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