3 * THE REGENTS OF THE UNIVERSITY OF MICHIGAN
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
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
32 * Portions Copyright (c) 2008
33 * The Linux Box Corporation
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
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.
61 #include <afsconfig.h>
62 #include "afs/param.h"
64 #if defined(AFS_CACHE_BYPASS)
66 #include "afs/afs_bypasscache.h"
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"
78 #if defined(AFS_LINUX26_ENV)
79 #define LockPage(pp) lock_page(pp)
80 #define UnlockPage(pp) unlock_page(pp)
82 #define AFS_KMAP_ATOMIC
85 #define afs_min(A,B) ((A)<(B)) ? (A) : (B)
88 /* conditional GLOCK macros */
89 #define COND_GLOCK(var) \
91 var = ISAFS_GLOCK(); \
96 #define COND_RE_GUNLOCK(var) \
103 /* conditional GUNLOCK macros */
105 #define COND_GUNLOCK(var) \
107 var = ISAFS_GLOCK(); \
112 #define COND_RE_GLOCK(var) \
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 ? */
123 extern afs_rwlock_t afs_xcbhash;
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.
134 afs_TransitionToBypass(register struct vcache *avc,
135 register afs_ucred_t *acred, int aflags)
139 struct vrequest treq;
146 if (avc->f.states & FCSBypass)
147 osi_Panic("afs_TransitionToBypass: illegal transition to bypass--already FCSBypass\n");
149 if (aflags & TRANSChangeDesiredBit)
151 if (aflags & TRANSSetManualBit)
154 #ifdef AFS_BOZONLOCK_ENV
155 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
159 ObtainWriteLock(&avc->lock, 925);
161 /* If we never cached this, just change state */
162 if (setDesire && (!avc->cachingStates & FCSBypass)) {
163 avc->f.states |= FCSBypass;
167 /* cg2v, try to store any chunks not written 20071204 */
168 if (avc->execsOrWriters > 0) {
169 code = afs_InitReq(&treq, acred);
171 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
175 /* also cg2v, don't dequeue the callback */
176 ObtainWriteLock(&afs_xcbhash, 956);
177 afs_DequeueCallback(avc);
178 ReleaseWriteLock(&afs_xcbhash);
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;
189 avc->cachingStates |= FCSBypass; /* Set the bypass flag */
191 avc->cachingStates |= FCSDesireBypass;
193 avc->cachingStates |= FCSManuallySet;
194 avc->cachingTransitions++;
197 ReleaseWriteLock(&avc->lock);
198 #ifdef AFS_BOZONLOCK_ENV
199 afs_BozonUnlock(&avc->pvnLock, avc);
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.
213 afs_TransitionToCaching(register struct vcache *avc,
214 register afs_ucred_t *acred,
223 if (!avc->f.states & FCSBypass)
224 osi_Panic("afs_TransitionToCaching: illegal transition to caching--already caching\n");
226 if (aflags & TRANSChangeDesiredBit)
228 if (aflags & TRANSSetManualBit)
231 #ifdef AFS_BOZONLOCK_ENV
232 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
236 ObtainWriteLock(&avc->lock, 926);
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;
251 avc->cachingStates &= ~(FCSBypass); /* Reset the bypass flag */
253 avc->cachingStates &= ~(FCSDesireBypass);
255 avc->cachingStates |= FCSManuallySet;
256 avc->cachingTransitions++;
258 ReleaseWriteLock(&avc->lock);
259 #ifdef AFS_BOZONLOCK_ENV
260 afs_BozonUnlock(&avc->pvnLock, avc);
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
270 #if defined(AFS_LINUX24_ENV)
271 #define unlock_pages(auio) \
273 struct iovec *ciov; \
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..."); \
282 if(pp != NULL && PageLocked(pp)) \
287 ciov = (auio->uio_iov + iovno); \
288 pp = (struct page*) ciov->iov_base; \
290 afs_warn("Pages Unlocked.\n"); \
294 #define unlock_pages(auio) \
297 #error AFS_CACHE_BYPASS not implemented on this platform
301 /* no-cache prefetch routine */
303 afs_NoCacheFetchProc(register struct rx_call *acall,
304 register struct vcache *avc,
305 register uio_t *auio,
306 afs_int32 release_pages,
312 int moredata, iovno, iovoff, iovmax, clen, result, locked;
316 #ifdef AFS_KMAP_ATOMIC
317 char *page_buffer = osi_Alloc(PAGE_SIZE);
319 char *page_buffer = NULL;
322 ciov = auio->uio_iov;
323 pp = (struct page*) ciov->iov_base;
324 iovmax = auio->uio_iovcnt - 1;
325 iovno = iovoff = result = 0;
328 COND_GUNLOCK(locked);
329 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
330 COND_RE_GLOCK(locked);
332 if (code != sizeof(afs_int32)) {
334 afs_warn("Preread error. code: %d instead of %d\n",
335 code, sizeof(afs_int32));
339 length = ntohl(length);
343 afs_warn("Preread error. Got length %d, which is greater than size %d\n",
350 * The fetch protocol is extended for the AFS/DFS translator
351 * to allow multiple blocks of data, each with its own length,
352 * to be returned. As long as the top bit is set, there are more
355 * We do not do this for AFS file servers because they sometimes
356 * return large negative numbers as the transfer size.
358 if (avc->f.states & CForeign) {
359 moredata = length & 0x80000000;
360 length &= ~0x80000000;
367 clen = ciov->iov_len - iovoff;
368 tlen = afs_min(length, clen);
369 #ifdef AFS_LINUX24_ENV
370 #ifndef AFS_KMAP_ATOMIC
374 /* rx doesn't provide an interface to simply advance
375 or consume n bytes. for now, allocate a PAGE_SIZE
376 region of memory to receive bytes in the case that
377 there were holes in readpages */
378 if(page_buffer == NULL)
379 page_buffer = osi_Alloc(PAGE_SIZE);
380 address = page_buffer;
383 address = page_buffer;
387 #error AFS_CACHE_BYPASS not implemented on this platform
390 COND_GUNLOCK(locked);
391 code = rx_Read(acall, address, tlen);
392 COND_RE_GLOCK(locked);
395 afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", code);
399 } else if (code == 0) {
401 afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
412 #ifdef AFS_LINUX24_ENV
413 #ifdef AFS_KMAP_ATOMIC
415 address = kmap_atomic(pp, KM_USER0);
416 memcpy(address, page_buffer, PAGE_SIZE);
417 kunmap_atomic(address, KM_USER0);
422 #error AFS_CACHE_BYPASS not implemented on this platform
424 #endif /* LINUX 24 */
425 /* we filled a page, conditionally release it */
426 if (release_pages && ciov->iov_base) {
427 /* this is appropriate when no caller intends to unlock
428 * and release the page */
429 #ifdef AFS_LINUX24_ENV
434 afs_warn("afs_NoCacheFetchProc: page not locked at iovno %d!\n", iovno);
435 #ifndef AFS_KMAP_ATOMIC
440 #error AFS_CACHE_BYPASS not implemented on this platform
444 /* and carry uio_iov */
449 ciov = (auio->uio_iov + iovno);
450 pp = (struct page*) ciov->iov_base;
458 osi_Free(page_buffer, PAGE_SIZE);
463 /* dispatch a no-cache read request */
465 afs_ReadNoCache(register struct vcache *avc,
466 register struct nocache_read_request *bparms,
471 struct brequest *breq;
472 struct vrequest *areq;
474 /* the reciever will free this */
475 areq = osi_Alloc(sizeof(struct vrequest));
477 if (avc && avc->vc_error) {
479 afs_warn("afs_ReadNoCache VCache Error!\n");
482 if ((code = afs_InitReq(areq, acred))) {
483 afs_warn("afs_ReadNoCache afs_InitReq error!\n");
488 code = afs_VerifyVCache(avc, areq);
492 code = afs_CheckCode(code, areq, 11); /* failed to get it */
493 afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
499 /* and queue this one */
503 breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1,
504 bparms, (void *)0, (void *)0);
509 afs_osi_Wait(10 * bcnt, 0, 0);
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.
525 #ifdef AFS_LINUX24_ENV
526 unlock_pages(bparms->auio);
529 #error AFS_CACHE_BYPASS not implemented on this platform
532 osi_Free(areq, sizeof(struct vrequest));
533 osi_Free(bparms->auio->uio_iov,
534 bparms->auio->uio_iovcnt * sizeof(struct iovec));
535 osi_Free(bparms->auio, sizeof(uio_t));
536 osi_Free(bparms, sizeof(struct nocache_read_request));
541 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
543 afs_PrefetchNoCache(register struct vcache *avc,
544 register afs_ucred_t *acred,
545 register struct nocache_read_request *bparms)
548 struct iovec *iovecp;
549 struct vrequest *areq;
551 #ifdef AFS_64BIT_CLIENT
552 afs_int32 length_hi, bytes, locked;
555 register struct afs_conn *tc;
557 struct rx_call *tcall;
559 struct AFSVolSync tsync;
560 struct AFSFetchStatus OutStatus;
561 struct AFSCallBack CallBack;
563 struct tlocal1 *tcallspec;
567 iovecp = auio->uio_iov;
569 tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
571 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
573 avc->callback = tc->srvr->server;
575 tcall = rx_NewCall(tc->id);
576 #ifdef AFS_64BIT_CLIENT
577 if (!afs_serverHasNo64Bit(tc)) {
578 code = StartRXAFS_FetchData64(tcall,
579 (struct AFSFid *) &avc->f.fid.Fid,
583 COND_GUNLOCK(locked);
584 bytes = rx_Read(tcall, (char *)&length_hi,
586 COND_RE_GLOCK(locked);
588 if (bytes != sizeof(afs_int32)) {
590 code = rx_Error(tcall);
591 COND_GUNLOCK(locked);
592 code = rx_EndCall(tcall, code);
593 COND_RE_GLOCK(locked);
597 } /* afs_serverHasNo64Bit */
598 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
599 if (auio->uio_offset > 0x7FFFFFFF) {
603 pos = auio->uio_offset;
604 COND_GUNLOCK(locked);
606 tcall = rx_NewCall(tc->id);
607 code = StartRXAFS_FetchData(tcall,
608 (struct AFSFid *) &avc->f.fid.Fid,
609 pos, bparms->length);
610 COND_RE_GLOCK(locked);
612 afs_serverSetNo64Bit(tc);
615 code = StartRXAFS_FetchData(tcall,
616 (struct AFSFid *) &avc->f.fid.Fid,
617 auio->uio_offset, bparms->length);
620 code = afs_NoCacheFetchProc(tcall, avc, auio,
621 1 /* release_pages */,
624 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
629 code = EndRXAFS_FetchData(tcall, &tcallspec->OutStatus,
630 &tcallspec->CallBack,
633 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
635 code = rx_EndCall(tcall, code);
637 afs_warn("BYPASS: No connection.\n");
639 #ifdef AFS_LINUX24_ENV
643 #error AFS_CACHE_BYPASS not implemented on this platform
648 } while (afs_Analyze(tc, code, &avc->f.fid, areq,
649 AFS_STATS_FS_RPCIDX_FETCHDATA,
653 * Copy appropriate fields into vcache
656 afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
658 osi_Free(areq, sizeof(struct vrequest));
659 osi_Free(tcallspec, sizeof(struct tlocal1));
660 osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
661 osi_Free(bparms, sizeof(struct nocache_read_request));
662 osi_Free(auio, sizeof(uio_t));
666 #endif /* AFS_CACHE_BYPASS */