Tidy up uio_t meaning
[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) && defined(AFS_LINUX24_ENV)
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 #ifndef afs_min
79 #define afs_min(A,B) ((A)<(B)) ? (A) : (B)
80 #endif
81
82 /* conditional GLOCK macros */
83 #define COND_GLOCK(var) \
84         do { \
85                 var = ISAFS_GLOCK(); \
86                 if(!var) \
87                         RX_AFS_GLOCK(); \
88         } while(0)
89
90 #define COND_RE_GUNLOCK(var) \
91         do { \
92                 if(var) \
93                         RX_AFS_GUNLOCK(); \
94         } while(0)
95
96
97 /* conditional GUNLOCK macros */
98
99 #define COND_GUNLOCK(var) \
100         do {    \
101                 var = ISAFS_GLOCK(); \
102                 if(var) \
103                         RX_AFS_GUNLOCK(); \
104         } while(0)
105
106 #define COND_RE_GLOCK(var) \
107         do { \
108                 if(var) \
109                         RX_AFS_GLOCK(); \
110         } while(0)
111
112
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 ? */
116
117 extern afs_rwlock_t afs_xcbhash;
118
119 /*
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.
126  */
127 void
128 afs_TransitionToBypass(struct vcache *avc,
129                        afs_ucred_t *acred, int aflags)
130 {
131
132     afs_int32 code;
133     struct vrequest treq;
134     int setDesire = 0;
135     int setManual = 0;
136
137     if (!avc)
138         return;
139
140     if (aflags & TRANSChangeDesiredBit)
141         setDesire = 1;
142     if (aflags & TRANSSetManualBit)
143         setManual = 1;
144
145 #ifdef AFS_BOZONLOCK_ENV
146     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
147 #else
148     AFS_GLOCK();
149 #endif
150
151     ObtainWriteLock(&avc->lock, 925);
152     /*
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.
155      */
156     if (avc->cachingStates & FCSBypass)
157         goto done;
158
159     /* If we never cached this, just change state */
160     if (setDesire && (!(avc->cachingStates & FCSBypass))) {
161         avc->cachingStates |= FCSBypass;
162         goto done;
163     }
164
165     /* cg2v, try to store any chunks not written 20071204 */
166     if (avc->execsOrWriters > 0) {
167         code = afs_InitReq(&treq, acred);
168         if (!code)
169             code = afs_StoreAllSegments(avc, &treq, AFS_SYNC | AFS_LASTSTORE);
170     }
171
172 #if 0
173     /* also cg2v, don't dequeue the callback */
174     ObtainWriteLock(&afs_xcbhash, 956);
175     afs_DequeueCallback(avc);
176     ReleaseWriteLock(&afs_xcbhash);
177 #endif
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;
185     }
186
187     avc->cachingStates |= FCSBypass;    /* Set the bypass flag */
188     if(setDesire)
189         avc->cachingStates |= FCSDesireBypass;
190     if(setManual)
191         avc->cachingStates |= FCSManuallySet;
192     avc->cachingTransitions++;
193
194 done:
195     ReleaseWriteLock(&avc->lock);
196 #ifdef AFS_BOZONLOCK_ENV
197     afs_BozonUnlock(&avc->pvnLock, avc);
198 #else
199     AFS_GUNLOCK();
200 #endif
201 }
202
203 /*
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.
209  */
210 void
211 afs_TransitionToCaching(struct vcache *avc,
212                         afs_ucred_t *acred,
213                         int aflags)
214 {
215     int resetDesire = 0;
216     int setManual = 0;
217
218     if (!avc)
219         return;
220
221     if (aflags & TRANSChangeDesiredBit)
222         resetDesire = 1;
223     if (aflags & TRANSSetManualBit)
224         setManual = 1;
225
226 #ifdef AFS_BOZONLOCK_ENV
227     afs_BozonLock(&avc->pvnLock, avc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
228 #else
229     AFS_GLOCK();
230 #endif
231     ObtainWriteLock(&avc->lock, 926);
232     /*
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.
235      */
236     if (!(avc->cachingStates & FCSBypass))
237         goto done;
238
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;
250     }
251
252     avc->cachingStates &= ~(FCSBypass);    /* Reset the bypass flag */
253     if (resetDesire)
254         avc->cachingStates &= ~(FCSDesireBypass);
255     if (setManual)
256         avc->cachingStates |= FCSManuallySet;
257     avc->cachingTransitions++;
258
259 done:
260     ReleaseWriteLock(&avc->lock);
261 #ifdef AFS_BOZONLOCK_ENV
262     afs_BozonUnlock(&avc->pvnLock, avc);
263 #else
264     AFS_GUNLOCK();
265 #endif
266 }
267
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
270  * to be unlocked.
271  */
272 #define unlock_and_release_pages(auio) \
273     do { \
274         struct iovec *ciov;     \
275         struct page *pp; \
276         afs_int32 iovmax; \
277         afs_int32 iovno = 0; \
278         ciov = auio->uio_iov; \
279         iovmax = auio->uio_iovcnt - 1;  \
280         pp = (struct page*) ciov->iov_base;     \
281         while(1) { \
282             if (pp) { \
283                 if (PageLocked(pp)) \
284                     unlock_page(pp);    \
285                 put_page(pp); /* decrement refcount */ \
286             } \
287             iovno++; \
288             if(iovno > iovmax) \
289                 break; \
290             ciov = (auio->uio_iov + iovno);     \
291             pp = (struct page*) ciov->iov_base; \
292         } \
293     } while(0)
294
295 /* no-cache prefetch routine */
296 static afs_int32
297 afs_NoCacheFetchProc(struct rx_call *acall,
298                      struct vcache *avc,
299                      struct uio *auio,
300                      afs_int32 release_pages,
301                      afs_int32 size)
302 {
303     afs_int32 length;
304     afs_int32 code;
305     int moredata, iovno, iovoff, iovmax, result, locked;
306     struct iovec *ciov;
307     struct iovec *rxiov;
308     int nio;
309     struct page *pp;
310     char *address;
311
312     int curpage, bytes;
313     int pageoff;
314
315     rxiov = osi_AllocSmallSpace(sizeof(struct iovec) * RX_MAXIOVECS);
316     ciov = auio->uio_iov;
317     pp = (struct page*) ciov->iov_base;
318     iovmax = auio->uio_iovcnt - 1;
319     iovno = iovoff = result = 0;
320
321     do {
322         COND_GUNLOCK(locked);
323         code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
324         COND_RE_GLOCK(locked);
325         if (code != sizeof(afs_int32)) {
326             result = 0;
327             afs_warn("Preread error. code: %d instead of %d\n",
328                 code, (int)sizeof(afs_int32));
329             unlock_and_release_pages(auio);
330             goto done;
331         } else
332             length = ntohl(length);
333
334         if (length > size) {
335             result = EIO;
336             afs_warn("Preread error. Got length %d, which is greater than size %d\n",
337                      length, size);
338             unlock_and_release_pages(auio);
339             goto done;
340         }
341
342         /* If we get a 0 length reply, time to cleanup and return */
343         if (length == 0) {
344             unlock_and_release_pages(auio);
345             result = 0;
346             goto done;
347         }
348
349         /*
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
353          * blocks expected.
354          *
355          * We do not do this for AFS file servers because they sometimes
356          * return large negative numbers as the transfer size.
357          */
358         if (avc->f.states & CForeign) {
359             moredata = length & 0x80000000;
360             length &= ~0x80000000;
361         } else {
362             moredata = 0;
363         }
364
365         for (curpage = 0; curpage <= iovmax; curpage++) {
366             pageoff = 0;
367             while (pageoff < 4096) {
368                 /* If no more iovs, issue new read. */
369                 if (iovno >= nio) {
370                     COND_GUNLOCK(locked);
371                     bytes = rx_Readv(acall, rxiov, &nio, RX_MAXIOVECS, length);
372                     COND_RE_GLOCK(locked);
373                     if (bytes < 0) {
374                         afs_warn("afs_NoCacheFetchProc: rx_Read error. Return code was %d\n", bytes);
375                         result = 0;
376                         unlock_and_release_pages(auio);
377                         goto done;
378                     } else if (bytes == 0) {
379                         result = 0;
380                         afs_warn("afs_NoCacheFetchProc: rx_Read returned zero. Aborting.\n");
381                         unlock_and_release_pages(auio);
382                         goto done;
383                     }
384                     length -= bytes;
385                     iovno = 0;
386                 }
387                 pp = (struct page *)auio->uio_iov[curpage].iov_base;
388                 if (pageoff + (rxiov[iovno].iov_len - iovoff) <= PAGE_CACHE_SIZE) {
389                     /* Copy entire (or rest of) current iovec into current page */
390                     if (pp) {
391                         address = kmap_atomic(pp, KM_USER0);
392                         memcpy(address + pageoff, rxiov[iovno].iov_base + iovoff,
393                                 rxiov[iovno].iov_len - iovoff);
394                         kunmap_atomic(address, KM_USER0);
395                     }
396                     pageoff += rxiov[iovno].iov_len - iovoff;
397                     iovno++;
398                     iovoff = 0;
399                 } else {
400                     /* Copy only what's needed to fill current page */
401                     if (pp) {
402                         address = kmap_atomic(pp, KM_USER0);
403                         memcpy(address + pageoff, rxiov[iovno].iov_base + iovoff,
404                                 PAGE_CACHE_SIZE - pageoff);
405                         kunmap_atomic(address, KM_USER0);
406                     }
407                     iovoff += PAGE_CACHE_SIZE - pageoff;
408                     pageoff = PAGE_CACHE_SIZE;
409                 }
410                 /* we filled a page, or this is the last page.  conditionally release it */
411                 if (pp && ((pageoff == PAGE_CACHE_SIZE && release_pages)
412                                 || (length == 0 && iovno >= nio))) {
413                     /* this is appropriate when no caller intends to unlock
414                      * and release the page */
415                     SetPageUptodate(pp);
416                     if(PageLocked(pp))
417                         unlock_page(pp);
418                     else
419                         afs_warn("afs_NoCacheFetchProc: page not locked!\n");
420                     put_page(pp); /* decrement refcount */
421                 }
422                 if (length == 0 && iovno >= nio)
423                     goto done;
424             }
425         }
426     } while (moredata);
427
428 done:
429     osi_FreeSmallSpace(rxiov);
430     return result;
431 }
432
433
434 /* dispatch a no-cache read request */
435 afs_int32
436 afs_ReadNoCache(struct vcache *avc,
437                 struct nocache_read_request *bparms,
438                 afs_ucred_t *acred)
439 {
440     afs_int32 code;
441     afs_int32 bcnt;
442     struct brequest *breq;
443     struct vrequest *areq;
444
445     /* the reciever will free this */
446     areq = osi_Alloc(sizeof(struct vrequest));
447
448     if (avc && avc->vc_error) {
449         code = EIO;
450         afs_warn("afs_ReadNoCache VCache Error!\n");
451         goto cleanup;
452     }
453     if ((code = afs_InitReq(areq, acred))) {
454         afs_warn("afs_ReadNoCache afs_InitReq error!\n");
455         goto cleanup;
456     }
457
458     AFS_GLOCK();
459     code = afs_VerifyVCache(avc, areq);
460     AFS_GUNLOCK();
461
462     if (code) {
463         code = afs_CheckCode(code, areq, 11);   /* failed to get it */
464         afs_warn("afs_ReadNoCache Failed to verify VCache!\n");
465         goto cleanup;
466     }
467
468     bparms->areq = areq;
469
470     /* and queue this one */
471     bcnt = 1;
472     AFS_GLOCK();
473     while(bcnt < 20) {
474         breq = afs_BQueue(BOP_FETCH_NOCACHE, avc, B_DONTWAIT, 0, acred, 1, 1,
475                           bparms, (void *)0, (void *)0);
476         if(breq != 0) {
477             code = 0;
478             break;
479         }
480         afs_osi_Wait(10 * bcnt, 0, 0);
481     }
482     AFS_GUNLOCK();
483
484     if(!breq) {
485         code = EBUSY;
486         goto cleanup;
487     }
488
489     return code;
490
491 cleanup:
492     /* If there's a problem before we queue the request, we need to
493      * do everything that would normally happen when the request was
494      * processed, like unlocking the pages and freeing memory.
495      */
496     unlock_and_release_pages(bparms->auio);
497     osi_Free(areq, sizeof(struct vrequest));
498     osi_Free(bparms->auio->uio_iov,
499              bparms->auio->uio_iovcnt * sizeof(struct iovec));
500     osi_Free(bparms->auio, sizeof(struct uio));
501     osi_Free(bparms, sizeof(struct nocache_read_request));
502     return code;
503 }
504
505
506 /* Cannot have static linkage--called from BPrefetch (afs_daemons) */
507 afs_int32
508 afs_PrefetchNoCache(struct vcache *avc,
509                     afs_ucred_t *acred,
510                     struct nocache_read_request *bparms)
511 {
512     struct uio *auio;
513     struct iovec *iovecp;
514     struct vrequest *areq;
515     afs_int32 code = 0;
516     struct rx_connection *rxconn;
517 #ifdef AFS_64BIT_CLIENT
518     afs_int32 length_hi, bytes, locked;
519 #endif
520
521     struct afs_conn *tc;
522     struct rx_call *tcall;
523     struct tlocal1 {
524         struct AFSVolSync tsync;
525         struct AFSFetchStatus OutStatus;
526         struct AFSCallBack CallBack;
527     };
528     struct tlocal1 *tcallspec;
529
530     auio = bparms->auio;
531     areq = bparms->areq;
532     iovecp = auio->uio_iov;
533
534     tcallspec = (struct tlocal1 *) osi_Alloc(sizeof(struct tlocal1));
535     do {
536         tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK /* ignored */, &rxconn);
537         if (tc) {
538             avc->callback = tc->parent->srvr->server;
539             tcall = rx_NewCall(rxconn);
540 #ifdef AFS_64BIT_CLIENT
541             if (!afs_serverHasNo64Bit(tc)) {
542                 code = StartRXAFS_FetchData64(tcall,
543                                               (struct AFSFid *) &avc->f.fid.Fid,
544                                               auio->uio_offset,
545                                               bparms->length);
546                 if (code == 0) {
547                     COND_GUNLOCK(locked);
548                     bytes = rx_Read(tcall, (char *)&length_hi,
549                                     sizeof(afs_int32));
550                     COND_RE_GLOCK(locked);
551
552                     if (bytes != sizeof(afs_int32)) {
553                         length_hi = 0;
554                         code = rx_Error(tcall);
555                         COND_GUNLOCK(locked);
556                         code = rx_EndCall(tcall, code);
557                         COND_RE_GLOCK(locked);
558                         tcall = NULL;
559                     }
560                 }
561             } /* afs_serverHasNo64Bit */
562             if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
563                 if (auio->uio_offset > 0x7FFFFFFF) {
564                     code = EFBIG;
565                 } else {
566                     afs_int32 pos;
567                     pos = auio->uio_offset;
568                     COND_GUNLOCK(locked);
569                     if (!tcall)
570                         tcall = rx_NewCall(rxconn);
571                     code = StartRXAFS_FetchData(tcall,
572                                         (struct AFSFid *) &avc->f.fid.Fid,
573                                         pos, bparms->length);
574                     COND_RE_GLOCK(locked);
575                 }
576                 afs_serverSetNo64Bit(tc);
577             }
578 #else
579             code = StartRXAFS_FetchData(tcall,
580                                         (struct AFSFid *) &avc->f.fid.Fid,
581                                         auio->uio_offset, bparms->length);
582 #endif
583             if (code == 0) {
584                 code = afs_NoCacheFetchProc(tcall, avc, auio,
585                                             1 /* release_pages */,
586                                             bparms->length);
587             } else {
588                 afs_warn("BYPASS: StartRXAFS_FetchData failed: %d\n", code);
589                 unlock_and_release_pages(auio);
590                 goto done;
591             }
592             if (code == 0) {
593                 code = EndRXAFS_FetchData(tcall, &tcallspec->OutStatus,
594                                           &tcallspec->CallBack,
595                                           &tcallspec->tsync);
596             } else {
597                 afs_warn("BYPASS: NoCacheFetchProc failed: %d\n", code);
598             }
599             code = rx_EndCall(tcall, code);
600         } else {
601             afs_warn("BYPASS: No connection.\n");
602             code = -1;
603             unlock_and_release_pages(auio);
604             goto done;
605         }
606     } while (afs_Analyze(tc, rxconn, code, &avc->f.fid, areq,
607                                                  AFS_STATS_FS_RPCIDX_FETCHDATA,
608                                                  SHARED_LOCK,0));
609 done:
610     /*
611      * Copy appropriate fields into vcache
612      */
613
614     afs_ProcessFS(avc, &tcallspec->OutStatus, areq);
615
616     osi_Free(areq, sizeof(struct vrequest));
617     osi_Free(tcallspec, sizeof(struct tlocal1));
618     osi_Free(iovecp, auio->uio_iovcnt * sizeof(struct iovec));
619     osi_Free(bparms, sizeof(struct nocache_read_request));
620     osi_Free(auio, sizeof(struct uio));
621     return code;
622 }
623
624 #endif /* AFS_CACHE_BYPASS && AFS_LINUX24_ENV */