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) && defined(AFS_LINUX24_ENV)
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"
79 #define afs_min(A,B) ((A)<(B)) ? (A) : (B)
82 /* conditional GLOCK macros */
83 #define COND_GLOCK(var) \
85 var = ISAFS_GLOCK(); \
90 #define COND_RE_GUNLOCK(var) \
97 /* conditional GUNLOCK macros */
99 #define COND_GUNLOCK(var) \
101 var = ISAFS_GLOCK(); \
106 #define COND_RE_GLOCK(var) \
113 int cache_bypass_strategy = NEVER_BYPASS_CACHE;
114 int cache_bypass_threshold = AFS_CACHE_BYPASS_DISABLED; /* file size > threshold triggers bypass */
115 int cache_bypass_prefetch = 1; /* Should we do prefetching ? */
117 extern afs_rwlock_t afs_xcbhash;
120 * This is almost exactly like the PFlush() routine in afs_pioctl.c,
121 * but that routine is static. We are about to change a file from
122 * normal caching to bypass it's caching. Therefore, we want to
123 * free up any cache space in use by the file, and throw out any
124 * existing VM pages for the file. We keep track of the number of
125 * times we go back and forth from caching to bypass.
128 afs_TransitionToBypass(struct vcache *avc,
129 afs_ucred_t *acred, int aflags)
133 struct vrequest treq;
140 if (aflags & TRANSChangeDesiredBit)
142 if (aflags & TRANSSetManualBit)
145 #ifdef AFS_BOZONLOCK_ENV
146 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
151 ObtainWriteLock(&avc->lock, 925);
153 * Someone may have beat us to doing the transition - we had no lock
154 * when we checked the flag earlier. No cause to panic, just return.
156 if (avc->f.states & FCSBypass)
159 /* If we never cached this, just change state */
160 if (setDesire && (!(avc->cachingStates & FCSBypass))) {
161 avc->f.states |= FCSBypass;
165 /* cg2v, try to store any chunks not written 20071204 */
166 if (avc->execsOrWriters > 0) {
167 code = afs_InitReq(&treq, acred);
169 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
173 /* also cg2v, don't dequeue the callback */
174 ObtainWriteLock(&afs_xcbhash, 956);
175 afs_DequeueCallback(avc);
176 ReleaseWriteLock(&afs_xcbhash);
178 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
179 /* now find the disk cache entries */
180 afs_TryToSmush(avc, acred, 1);
181 osi_dnlc_purgedp(avc);
182 if (avc->linkData && !(avc->f.states & CCore)) {
183 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
184 avc->linkData = NULL;
187 avc->cachingStates |= FCSBypass; /* Set the bypass flag */
189 avc->cachingStates |= FCSDesireBypass;
191 avc->cachingStates |= FCSManuallySet;
192 avc->cachingTransitions++;
195 ReleaseWriteLock(&avc->lock);
196 #ifdef AFS_BOZONLOCK_ENV
197 afs_BozonUnlock(&avc->pvnLock, avc);
204 * This is almost exactly like the PFlush() routine in afs_pioctl.c,
205 * but that routine is static. We are about to change a file from
206 * bypassing caching to normal caching. Therefore, we want to
207 * throw out any existing VM pages for the file. We keep track of
208 * the number of times we go back and forth from caching to bypass.
211 afs_TransitionToCaching(struct vcache *avc,
221 if (aflags & TRANSChangeDesiredBit)
223 if (aflags & TRANSSetManualBit)
226 #ifdef AFS_BOZONLOCK_ENV
227 afs_BozonLock(&avc->pvnLock, avc); /* Since afs_TryToSmush will do a pvn_vptrunc */
231 ObtainWriteLock(&avc->lock, 926);
233 * Someone may have beat us to doing the transition - we had no lock
234 * when we checked the flag earlier. No cause to panic, just return.
236 if (!(avc->f.states & FCSBypass))
239 /* Ok, we actually do need to flush */
240 ObtainWriteLock(&afs_xcbhash, 957);
241 afs_DequeueCallback(avc);
242 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat cache entry */
243 ReleaseWriteLock(&afs_xcbhash);
244 /* now find the disk cache entries */
245 afs_TryToSmush(avc, acred, 1);
246 osi_dnlc_purgedp(avc);
247 if (avc->linkData && !(avc->f.states & CCore)) {
248 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
249 avc->linkData = NULL;
252 avc->cachingStates &= ~(FCSBypass); /* Reset the bypass flag */
254 avc->cachingStates &= ~(FCSDesireBypass);
256 avc->cachingStates |= FCSManuallySet;
257 avc->cachingTransitions++;
260 ReleaseWriteLock(&avc->lock);
261 #ifdef AFS_BOZONLOCK_ENV
262 afs_BozonUnlock(&avc->pvnLock, avc);
268 /* In the case where there's an error in afs_NoCacheFetchProc or
269 * afs_PrefetchNoCache, all of the pages they've been passed need
272 #define unlock_and_release_pages(auio) \
274 struct iovec *ciov; \
277 afs_int32 iovno = 0; \
278 ciov = auio->uio_iov; \
279 iovmax = auio->uio_iovcnt - 1; \
280 pp = (struct page*) ciov->iov_base; \
283 if (PageLocked(pp)) \
285 put_page(pp); /* decrement refcount */ \
290 ciov = (auio->uio_iov + iovno); \
291 pp = (struct page*) ciov->iov_base; \
295 /* no-cache prefetch routine */
297 afs_NoCacheFetchProc(struct rx_call *acall,
300 afs_int32 release_pages,
306 int moredata, iovno, iovoff, iovmax, clen, result, locked;
310 char *page_buffer = osi_Alloc(PAGE_SIZE);
312 ciov = auio->uio_iov;
313 pp = (struct page*) ciov->iov_base;
314 iovmax = auio->uio_iovcnt - 1;
315 iovno = iovoff = result = 0;
318 COND_GUNLOCK(locked);
319 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
320 COND_RE_GLOCK(locked);
322 if (code != sizeof(afs_int32)) {
324 afs_warn("Preread error. code: %d instead of %d\n",
325 code, (int)sizeof(afs_int32));
326 unlock_and_release_pages(auio);
329 length = ntohl(length);
333 afs_warn("Preread error. Got length %d, which is greater than size %d\n",
335 unlock_and_release_pages(auio);
339 /* If we get a 0 length reply, time to cleanup and return */
341 unlock_and_release_pages(auio);
347 * The fetch protocol is extended for the AFS/DFS translator
348 * to allow multiple blocks of data, each with its own length,
349 * to be returned. As long as the top bit is set, there are more
352 * We do not do this for AFS file servers because they sometimes
353 * return large negative numbers as the transfer size.
355 if (avc->f.states & CForeign) {
356 moredata = length & 0x80000000;
357 length &= ~0x80000000;
364 clen = ciov->iov_len - iovoff;
365 tlen = afs_min(length, clen);
366 address = page_buffer;
367 COND_GUNLOCK(locked);
368 code = rx_Read(acall, address, tlen);
369 COND_RE_GLOCK(locked);
372 afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", code);
374 unlock_and_release_pages(auio);
376 } else if (code == 0) {
378 afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
379 unlock_and_release_pages(auio);
390 address = kmap_atomic(pp, KM_USER0);
391 memcpy(address, page_buffer, PAGE_SIZE);
392 kunmap_atomic(address, KM_USER0);
394 /* we filled a page, conditionally release it */
395 if (release_pages && ciov->iov_base) {
396 /* this is appropriate when no caller intends to unlock
397 * and release the page */
402 afs_warn("afs_NoCacheFetchProc: page not locked at iovno %d!\n", iovno);
403 put_page(pp); /* decrement refcount */
405 /* and carry uio_iov */
410 ciov = (auio->uio_iov + iovno);
411 pp = (struct page*) ciov->iov_base;
419 osi_Free(page_buffer, PAGE_SIZE);
424 /* dispatch a no-cache read request */
426 afs_ReadNoCache(struct vcache *avc,
427 struct nocache_read_request *bparms,
432 struct brequest *breq;
433 struct vrequest *areq;
435 /* the reciever will free this */
436 areq = osi_Alloc(sizeof(struct vrequest));
438 if (avc && avc->vc_error) {
440 afs_warn("afs_ReadNoCache VCache Error!\n");
443 if ((code = afs_InitReq(areq, acred))) {
444 afs_warn("afs_ReadNoCache afs_InitReq error!\n");
449 code = afs_VerifyVCache(avc, areq);
453 code = afs_CheckCode(code, areq, 11); /* failed to get it */
454 afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
460 /* and queue this one */
464 breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1,
465 bparms, (void *)0, (void *)0);
470 afs_osi_Wait(10 * bcnt, 0, 0);
482 /* If there's a problem before we queue the request, we need to
483 * do everything that would normally happen when the request was
484 * processed, like unlocking the pages and freeing memory.
486 unlock_and_release_pages(bparms->auio);
487 osi_Free(areq, sizeof(struct vrequest));
488 osi_Free(bparms->auio->uio_iov,
489 bparms->auio->uio_iovcnt * sizeof(struct iovec));
490 osi_Free(bparms->auio, sizeof(uio_t));
491 osi_Free(bparms, sizeof(struct nocache_read_request));
496 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
498 afs_PrefetchNoCache(struct vcache *avc,
500 struct nocache_read_request *bparms)
503 struct iovec *iovecp;
504 struct vrequest *areq;
506 #ifdef AFS_64BIT_CLIENT
507 afs_int32 length_hi, bytes, locked;
512 struct rx_call *tcall;
514 struct AFSVolSync tsync;
515 struct AFSFetchStatus OutStatus;
516 struct AFSCallBack CallBack;
518 struct tlocal1 *tcallspec;
522 iovecp = auio->uio_iov;
524 tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
526 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */);
528 avc->callback = tc->srvr->server;
530 tcall = rx_NewCall(tc->id);
531 #ifdef AFS_64BIT_CLIENT
532 if (!afs_serverHasNo64Bit(tc)) {
533 code = StartRXAFS_FetchData64(tcall,
534 (struct AFSFid *) &avc->f.fid.Fid,
538 COND_GUNLOCK(locked);
539 bytes = rx_Read(tcall, (char *)&length_hi,
541 COND_RE_GLOCK(locked);
543 if (bytes != sizeof(afs_int32)) {
545 code = rx_Error(tcall);
546 COND_GUNLOCK(locked);
547 code = rx_EndCall(tcall, code);
548 COND_RE_GLOCK(locked);
552 } /* afs_serverHasNo64Bit */
553 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
554 if (auio->uio_offset > 0x7FFFFFFF) {
558 pos = auio->uio_offset;
559 COND_GUNLOCK(locked);
561 tcall = rx_NewCall(tc->id);
562 code = StartRXAFS_FetchData(tcall,
563 (struct AFSFid *) &avc->f.fid.Fid,
564 pos, bparms->length);
565 COND_RE_GLOCK(locked);
567 afs_serverSetNo64Bit(tc);
570 code = StartRXAFS_FetchData(tcall,
571 (struct AFSFid *) &avc->f.fid.Fid,
572 auio->uio_offset, bparms->length);
575 code = afs_NoCacheFetchProc(tcall, avc, auio,
576 1 /* release_pages */,
579 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
580 unlock_and_release_pages(auio);
584 code = EndRXAFS_FetchData(tcall, &tcallspec->OutStatus,
585 &tcallspec->CallBack,
588 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
590 code = rx_EndCall(tcall, code);
592 afs_warn("BYPASS: No connection.\n");
594 unlock_and_release_pages(auio);
597 } while (afs_Analyze(tc, code, &avc->f.fid, areq,
598 AFS_STATS_FS_RPCIDX_FETCHDATA,
602 * Copy appropriate fields into vcache
605 afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
607 osi_Free(areq, sizeof(struct vrequest));
608 osi_Free(tcallspec, sizeof(struct tlocal1));
609 osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
610 osi_Free(bparms, sizeof(struct nocache_read_request));
611 osi_Free(auio, sizeof(uio_t));
615 #endif /* AFS_CACHE_BYPASS && AFS_LINUX24_ENV */