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.
133 void afs_TransitionToBypass(register struct vcache *avc, register struct AFS_UCRED *acred, int aflags)
137 struct vrequest treq;
144 if(avc->f.states & FCSBypass)
145 osi_Panic("afs_TransitionToBypass: illegal transition to bypass--already FCSBypass\n");
147 if(aflags & TRANSChangeDesiredBit)
149 if(aflags & TRANSSetManualBit)
152 #ifdef AFS_BOZONLOCK_ENV
153 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
157 ObtainWriteLock(&avc->lock, 925);
159 /* If we never cached this, just change state */
160 if(setDesire && (!avc->cachingStates & FCSBypass)) {
161 avc->f.states |= FCSBypass;
164 /* cg2v, try to store any chunks not written 20071204 */
165 if (avc->execsOrWriters > 0) {
166 code = afs_InitReq(&treq, acred);
168 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
171 /* also cg2v, don't dequeue the callback */
172 ObtainWriteLock(&afs_xcbhash, 956);
173 afs_DequeueCallback(avc);
174 ReleaseWriteLock(&afs_xcbhash);
176 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat cache entry */
177 /* now find the disk cache entries */
178 afs_TryToSmush(avc, acred, 1);
179 osi_dnlc_purgedp(avc);
180 if (avc->linkData && !(avc->f.states & CCore)) {
181 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
182 avc->linkData = NULL;
185 avc->cachingStates |= FCSBypass; /* Set the bypass flag */
187 avc->cachingStates |= FCSDesireBypass;
189 avc->cachingStates |= FCSManuallySet;
190 avc->cachingTransitions++;
193 ReleaseWriteLock(&avc->lock);
194 #ifdef AFS_BOZONLOCK_ENV
195 afs_BozonUnlock(&avc->pvnLock, avc);
202 * This is almost exactly like the PFlush() routine in afs_pioctl.c,
203 * but that routine is static. We are about to change a file from
204 * bypassing caching to normal caching. Therefore, we want to
205 * throw out any existing VM pages for the file. We keep track of
206 * the number of times we go back and forth from caching to bypass.
208 void afs_TransitionToCaching(register struct vcache *avc, register struct AFS_UCRED *acred, int aflags)
216 if(!avc->f.states & FCSBypass)
217 osi_Panic("afs_TransitionToCaching: illegal transition to caching--already caching\n");
219 if(aflags & TRANSChangeDesiredBit)
221 if(aflags & TRANSSetManualBit)
224 #ifdef AFS_BOZONLOCK_ENV
225 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
229 ObtainWriteLock(&avc->lock, 926);
231 /* Ok, we actually do need to flush */
232 ObtainWriteLock(&afs_xcbhash, 957);
233 afs_DequeueCallback(avc);
234 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat cache entry */
235 ReleaseWriteLock(&afs_xcbhash);
236 /* now find the disk cache entries */
237 afs_TryToSmush(avc, acred, 1);
238 osi_dnlc_purgedp(avc);
239 if (avc->linkData && !(avc->f.states & CCore)) {
240 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
241 avc->linkData = NULL;
244 avc->cachingStates &= ~(FCSBypass); /* Reset the bypass flag */
246 avc->cachingStates &= ~(FCSDesireBypass);
248 avc->cachingStates |= FCSManuallySet;
249 avc->cachingTransitions++;
251 ReleaseWriteLock(&avc->lock);
252 #ifdef AFS_BOZONLOCK_ENV
253 afs_BozonUnlock(&avc->pvnLock, avc);
259 /* In the case where there's an error in afs_NoCacheFetchProc or
260 * afs_PrefetchNoCache, all of the pages they've been passed need
263 #if defined(AFS_LINUX24_ENV)
264 #define unlock_pages(auio) \
266 struct iovec *ciov; \
269 afs_int32 iovno = 0; \
270 ciov = auio->uio_iov; \
271 iovmax = auio->uio_iovcnt - 1; \
272 pp = (struct page*) ciov->iov_base; \
273 afs_warn("BYPASS: Unlocking pages..."); \
275 if(pp != NULL && PageLocked(pp)) \
280 ciov = (auio->uio_iov + iovno); \
281 pp = (struct page*) ciov->iov_base; \
283 afs_warn("Pages Unlocked.\n"); \
287 #define unlock_pages(auio) \
290 #error AFS_CACHE_BYPASS not implemented on this platform
294 /* no-cache prefetch routine */
296 afs_NoCacheFetchProc(register struct rx_call *acall,
297 register struct vcache *avc,
298 register uio_t *auio,
299 afs_int32 release_pages)
304 int moredata, iovno, iovoff, iovmax, clen, result, locked;
308 #ifdef AFS_KMAP_ATOMIC
309 char *page_buffer = osi_Alloc(PAGE_SIZE);
311 char *page_buffer = NULL;
314 ciov = auio->uio_iov;
315 pp = (struct page*) ciov->iov_base;
316 iovmax = auio->uio_iovcnt - 1;
317 iovno = iovoff = result = 0;
320 COND_GUNLOCK(locked);
321 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
322 COND_RE_GLOCK(locked);
324 if (code != sizeof(afs_int32)) {
326 afs_warn("Preread error. code: %d instead of %d\n",
327 code, sizeof(afs_int32));
331 length = ntohl(length);
334 * The fetch protocol is extended for the AFS/DFS translator
335 * to allow multiple blocks of data, each with its own length,
336 * to be returned. As long as the top bit is set, there are more
339 * We do not do this for AFS file servers because they sometimes
340 * return large negative numbers as the transfer size.
342 if (avc->f.states & CForeign) {
343 moredata = length & 0x80000000;
344 length &= ~0x80000000;
351 clen = ciov->iov_len - iovoff;
352 tlen = afs_min(length, clen);
353 #ifdef AFS_LINUX24_ENV
354 #ifndef AFS_KMAP_ATOMIC
358 /* rx doesn't provide an interface to simply advance
359 or consume n bytes. for now, allocate a PAGE_SIZE
360 region of memory to receive bytes in the case that
361 there were holes in readpages */
362 if(page_buffer == NULL)
363 page_buffer = osi_Alloc(PAGE_SIZE);
364 address = page_buffer;
367 address = page_buffer;
371 #error AFS_CACHE_BYPASS not implemented on this platform
374 COND_GUNLOCK(locked);
375 code = rx_Read(acall, address, tlen);
376 COND_RE_GLOCK(locked);
379 afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", code);
383 } else if (code == 0) {
385 afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
397 #ifdef AFS_LINUX24_ENV
398 #ifdef AFS_KMAP_ATOMIC
400 address = kmap_atomic(pp, KM_USER0);
401 memcpy(address, page_buffer, PAGE_SIZE);
402 kunmap_atomic(address, KM_USER0);
407 #error AFS_CACHE_BYPASS not implemented on this platform
409 #endif /* LINUX 24 */
410 /* we filled a page, conditionally release it */
411 if(release_pages && ciov->iov_base) {
412 /* this is appropriate when no caller intends to unlock
413 * and release the page */
414 #ifdef AFS_LINUX24_ENV
419 afs_warn("afs_NoCacheFetchProc: page not locked at iovno %d!\n", iovno);
421 #ifndef AFS_KMAP_ATOMIC
426 #error AFS_CACHE_BYPASS not implemented on this platform
430 /* and carry uio_iov */
432 if(iovno > iovmax) goto done;
434 ciov = (auio->uio_iov + iovno);
435 pp = (struct page*) ciov->iov_base;
443 osi_Free(page_buffer, PAGE_SIZE);
448 /* dispatch a no-cache read request */
450 afs_ReadNoCache(register struct vcache *avc,
451 register struct nocache_read_request *bparms,
452 struct AFS_UCRED *acred)
456 struct brequest *breq;
457 struct vrequest *areq;
459 /* the reciever will free this */
460 areq = osi_Alloc(sizeof(struct vrequest));
462 if (avc && avc->vc_error) {
464 afs_warn("afs_ReadNoCache VCache Error!\n");
467 if ((code = afs_InitReq(areq, acred))) {
468 afs_warn("afs_ReadNoCache afs_InitReq error!\n");
473 code = afs_VerifyVCache(avc, areq);
477 code = afs_CheckCode(code, areq, 11); /* failed to get it */
478 afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
484 /* and queue this one */
488 breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1, bparms);
493 afs_osi_Wait(10 * bcnt, 0, 0);
505 /* If there's a problem before we queue the request, we need to
506 * do everything that would normally happen when the request was
507 * processed, like unlocking the pages and freeing memory.
509 #ifdef AFS_LINUX24_ENV
510 unlock_pages(bparms->auio);
513 #error AFS_CACHE_BYPASS not implemented on this platform
516 osi_Free(areq, sizeof(struct vrequest));
517 osi_Free(bparms->auio->uio_iov, bparms->auio->uio_iovcnt * sizeof(struct iovec));
518 osi_Free(bparms->auio, sizeof(uio_t));
519 osi_Free(bparms, sizeof(struct nocache_read_request));
525 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
527 afs_PrefetchNoCache(register struct vcache *avc,
528 register struct AFS_UCRED *acred,
529 register struct nocache_read_request *bparms)
532 struct iovec *iovecp;
533 struct vrequest *areq;
534 afs_int32 code, length_hi, bytes, locked;
536 register struct afs_conn *tc;
538 struct rx_call *tcall;
540 struct AFSVolSync tsync;
541 struct AFSFetchStatus OutStatus;
542 struct AFSCallBack CallBack;
544 struct tlocal1 *tcallspec;
548 iovecp = auio->uio_iov;
550 tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
552 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
554 avc->callback = tc->srvr->server;
556 tcall = rx_NewCall(tc->id);
557 #ifdef AFS_64BIT_CLIENT
558 if(!afs_serverHasNo64Bit(tc)) {
559 code = StartRXAFS_FetchData64(tcall,
560 (struct AFSFid *) &avc->f.fid.Fid,
565 COND_GUNLOCK(locked);
566 bytes = rx_Read(tcall, (char *)&length_hi, sizeof(afs_int32));
567 COND_RE_GLOCK(locked);
569 if (bytes != sizeof(afs_int32)) {
571 code = rx_Error(tcall);
572 COND_GUNLOCK(locked);
573 code = rx_EndCall(tcall, code);
574 COND_RE_GLOCK(locked);
575 tcall = (struct rx_call *)0;
578 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
579 if (auio->uio_offset > 0x7FFFFFFF) {
583 pos = auio->uio_offset;
584 COND_GUNLOCK(locked);
586 tcall = rx_NewCall(tc->id);
587 code = StartRXAFS_FetchData(tcall,
588 (struct AFSFid *) &avc->f.fid.Fid,
589 pos, bparms->length);
590 COND_RE_GLOCK(locked);
592 afs_serverSetNo64Bit(tc);
594 } /* afs_serverHasNo64Bit */
596 code = StartRXAFS_FetchData(tcall,
597 (struct AFSFid *) &avc->f.fid.Fid,
598 auio->uio_offset, bparms->length);
602 code = afs_NoCacheFetchProc(tcall, avc, auio,
603 1 /* release_pages */);
605 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
610 code = EndRXAFS_FetchData(tcall,
611 &tcallspec->OutStatus,
612 &tcallspec->CallBack,
615 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
617 code = rx_EndCall(tcall, code);
620 afs_warn("BYPASS: No connection.\n");
622 #ifdef AFS_LINUX24_ENV
626 #error AFS_CACHE_BYPASS not implemented on this platform
631 } while (afs_Analyze(tc, code, &avc->f.fid, areq,
632 AFS_STATS_FS_RPCIDX_FETCHDATA,
636 * Copy appropriate fields into vcache
639 afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
641 osi_Free(areq, sizeof(struct vrequest));
642 osi_Free(tcallspec, sizeof(struct tlocal1));
643 osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
644 osi_Free(bparms, sizeof(struct nocache_read_request));
645 osi_Free(auio, sizeof(uio_t));
649 #endif /* AFS_CACHE_BYPASS */