Always unlock pages when returning from writepage
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Linux specific vnodeops. Also includes the glue routines required to call
12  * AFS vnodeops.
13  *
14  * So far the only truly scary part is that Linux relies on the inode cache
15  * to be up to date. Don't you dare break a callback and expect an fstat
16  * to give you meaningful information. This appears to be fixed in the 2.1
17  * development kernels. As it is we can fix this now by intercepting the 
18  * stat calls.
19  */
20
21 #include <afsconfig.h>
22 #include "afs/param.h"
23
24
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
28 #include <linux/mm.h>
29 #ifdef HAVE_MM_INLINE_H
30 #include <linux/mm_inline.h>
31 #endif
32 #include <linux/pagemap.h>
33 #include <linux/smp_lock.h>
34 #include <linux/writeback.h>
35 #include <linux/pagevec.h>
36 #if defined(AFS_CACHE_BYPASS)
37 #include "afs/lock.h"
38 #include "afs/afs_bypasscache.h"
39 #endif
40
41 #include "osi_compat.h"
42 #include "osi_pagecopy.h"
43
44 #ifdef pgoff2loff
45 #define pageoff(pp) pgoff2loff((pp)->index)
46 #else
47 #define pageoff(pp) pp->offset
48 #endif
49
50 #ifndef HAVE_PAGEVEC_LRU_ADD_FILE
51 #define __pagevec_lru_add_file __pagevec_lru_add
52 #endif
53
54 #ifndef MAX_ERRNO
55 #define MAX_ERRNO 1000L
56 #endif
57
58 #define LockPage(pp) lock_page(pp)
59 #define UnlockPage(pp) unlock_page(pp)
60 extern struct backing_dev_info afs_backing_dev_info;
61
62 extern struct vcache *afs_globalVp;
63 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
64 /* Some uses of BKL are perhaps not needed for bypass or memcache--
65  * why don't we try it out? */
66 extern struct afs_cacheOps afs_UfsCacheOps;
67
68 static inline void
69 afs_maybe_lock_kernel(void) {
70     if(afs_cacheType == &afs_UfsCacheOps)
71         lock_kernel();
72 }
73
74 static inline void
75 afs_maybe_unlock_kernel(void) {
76     if(afs_cacheType == &afs_UfsCacheOps)
77         unlock_kernel();
78 }
79
80 /* This function converts a positive error code from AFS into a negative
81  * code suitable for passing into the Linux VFS layer. It checks that the
82  * error code is within the permissable bounds for the ERR_PTR mechanism.
83  *
84  * _All_ error codes which come from the AFS layer should be passed through
85  * this function before being returned to the kernel.
86  */
87
88 static inline int
89 afs_convert_code(int code) {
90     if ((code >= 0) && (code <= MAX_ERRNO))
91         return -code;
92     else
93         return -EIO;
94 }
95
96 /* Linux doesn't require a credp for many functions, and crref is an expensive
97  * operation. This helper function avoids obtaining it for VerifyVCache calls
98  */
99
100 static inline int
101 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
102     cred_t *credp = NULL;
103     struct vrequest treq;
104     int code;
105
106     if (avc->f.states & CStatd) {
107         if (retcred)
108             *retcred = NULL;
109         return 0;
110     }
111
112     credp = crref();
113
114     code = afs_InitReq(&treq, credp);
115     if (code == 0)
116         code = afs_VerifyVCache2(avc, &treq);
117
118     if (retcred != NULL)
119         *retcred = credp;
120     else
121         crfree(credp);
122
123     return afs_convert_code(code);
124 }
125
126 static ssize_t
127 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
128 {
129     ssize_t code = 0;
130     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
131
132     AFS_GLOCK();
133     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
134                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
135                99999);
136     code = afs_linux_VerifyVCache(vcp, NULL);
137
138     if (code == 0) {
139         /* Linux's FlushPages implementation doesn't ever use credp,
140          * so we optimise by not using it */
141         osi_FlushPages(vcp, NULL);      /* ensure stale pages are gone */
142         AFS_GUNLOCK();
143         code = do_sync_read(fp, buf, count, offp);
144         AFS_GLOCK();
145     }
146
147     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
148                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
149                code);
150     AFS_GUNLOCK();
151     return code;
152 }
153
154
155 /* Now we have integrated VM for writes as well as reads. generic_file_write
156  * also takes care of re-positioning the pointer if file is open in append
157  * mode. Call fake open/close to ensure we do writes of core dumps.
158  */
159 static ssize_t
160 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
161 {
162     ssize_t code = 0;
163     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
164     cred_t *credp;
165
166     AFS_GLOCK();
167
168     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
169                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
170                (fp->f_flags & O_APPEND) ? 99998 : 99999);
171
172     code = afs_linux_VerifyVCache(vcp, &credp);
173
174     ObtainWriteLock(&vcp->lock, 529);
175     afs_FakeOpen(vcp);
176     ReleaseWriteLock(&vcp->lock);
177     if (code == 0) {
178             AFS_GUNLOCK();
179             code = do_sync_write(fp, buf, count, offp);
180             AFS_GLOCK();
181     }
182
183     ObtainWriteLock(&vcp->lock, 530);
184
185     if (vcp->execsOrWriters == 1 && !credp)
186       credp = crref();
187
188     afs_FakeClose(vcp, credp);
189     ReleaseWriteLock(&vcp->lock);
190
191     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
192                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
193                code);
194
195     if (credp)
196       crfree(credp);
197     AFS_GUNLOCK();
198     return code;
199 }
200
201 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
202
203 /* This is a complete rewrite of afs_readdir, since we can make use of
204  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
205  * handling and use of bulkstats will need to be reflected here as well.
206  */
207 static int
208 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
209 {
210     struct vcache *avc = VTOAFS(FILE_INODE(fp));
211     struct vrequest treq;
212     register struct dcache *tdc;
213     int code;
214     int offset;
215     int dirpos;
216     struct DirEntry *de;
217     ino_t ino;
218     int len;
219     afs_size_t origOffset, tlen;
220     cred_t *credp = crref();
221     struct afs_fakestat_state fakestat;
222
223     afs_maybe_lock_kernel();
224     AFS_GLOCK();
225     AFS_STATCNT(afs_readdir);
226
227     code = afs_convert_code(afs_InitReq(&treq, credp));
228     crfree(credp);
229     if (code)
230         goto out1;
231
232     afs_InitFakeStat(&fakestat);
233     code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
234     if (code)
235         goto out;
236
237     /* update the cache entry */
238   tagain:
239     code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
240     if (code)
241         goto out;
242
243     /* get a reference to the entire directory */
244     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
245     len = tlen;
246     if (!tdc) {
247         code = -ENOENT;
248         goto out;
249     }
250     ObtainSharedLock(&avc->lock, 810);
251     UpgradeSToWLock(&avc->lock, 811);
252     ObtainReadLock(&tdc->lock);
253     /*
254      * Make sure that the data in the cache is current. There are two
255      * cases we need to worry about:
256      * 1. The cache data is being fetched by another process.
257      * 2. The cache data is no longer valid
258      */
259     while ((avc->f.states & CStatd)
260            && (tdc->dflags & DFFetching)
261            && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
262         ReleaseReadLock(&tdc->lock);
263         ReleaseSharedLock(&avc->lock);
264         afs_osi_Sleep(&tdc->validPos);
265         ObtainSharedLock(&avc->lock, 812);
266         ObtainReadLock(&tdc->lock);
267     }
268     if (!(avc->f.states & CStatd)
269         || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
270         ReleaseReadLock(&tdc->lock);
271         ReleaseSharedLock(&avc->lock);
272         afs_PutDCache(tdc);
273         goto tagain;
274     }
275
276     /* Set the readdir-in-progress flag, and downgrade the lock
277      * to shared so others will be able to acquire a read lock.
278      */
279     avc->f.states |= CReadDir;
280     avc->dcreaddir = tdc;
281     avc->readdir_pid = MyPidxx2Pid(MyPidxx);
282     ConvertWToSLock(&avc->lock);
283
284     /* Fill in until we get an error or we're done. This implementation
285      * takes an offset in units of blobs, rather than bytes.
286      */
287     code = 0;
288     offset = (int) fp->f_pos;
289     while (1) {
290         dirpos = BlobScan(tdc, offset);
291         if (!dirpos)
292             break;
293
294         de = afs_dir_GetBlob(tdc, dirpos);
295         if (!de)
296             break;
297
298         ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
299
300         if (de->name)
301             len = strlen(de->name);
302         else {
303             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
304                    (unsigned long)&tdc->f.inode, dirpos);
305             DRelease(de, 0);
306             ReleaseSharedLock(&avc->lock);
307             afs_PutDCache(tdc);
308             code = -ENOENT;
309             goto out;
310         }
311
312         /* filldir returns -EINVAL when the buffer is full. */
313         {
314             unsigned int type = DT_UNKNOWN;
315             struct VenusFid afid;
316             struct vcache *tvc;
317             int vtype;
318             afid.Cell = avc->f.fid.Cell;
319             afid.Fid.Volume = avc->f.fid.Fid.Volume;
320             afid.Fid.Vnode = ntohl(de->fid.vnode);
321             afid.Fid.Unique = ntohl(de->fid.vunique);
322             if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
323                 type = DT_DIR;
324             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
325                 if (tvc->mvstat) {
326                     type = DT_DIR;
327                 } else if (((tvc->f.states) & (CStatd | CTruth))) {
328                     /* CTruth will be set if the object has
329                      *ever* been statd */
330                     vtype = vType(tvc);
331                     if (vtype == VDIR)
332                         type = DT_DIR;
333                     else if (vtype == VREG)
334                         type = DT_REG;
335                     /* Don't do this until we're sure it can't be a mtpt */
336                     /* else if (vtype == VLNK)
337                      * type=DT_LNK; */
338                     /* what other types does AFS support? */
339                 }
340                 /* clean up from afs_FindVCache */
341                 afs_PutVCache(tvc);
342             }
343             /* 
344              * If this is NFS readdirplus, then the filler is going to
345              * call getattr on this inode, which will deadlock if we're
346              * holding the GLOCK.
347              */
348             AFS_GUNLOCK();
349             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
350             AFS_GLOCK();
351         }
352         DRelease(de, 0);
353         if (code)
354             break;
355         offset = dirpos + 1 + ((len + 16) >> 5);
356     }
357     /* If filldir didn't fill in the last one this is still pointing to that
358      * last attempt.
359      */
360     fp->f_pos = (loff_t) offset;
361
362     ReleaseReadLock(&tdc->lock);
363     afs_PutDCache(tdc);
364     UpgradeSToWLock(&avc->lock, 813);
365     avc->f.states &= ~CReadDir;
366     avc->dcreaddir = 0;
367     avc->readdir_pid = 0;
368     ReleaseSharedLock(&avc->lock);
369     code = 0;
370
371 out:
372     afs_PutFakeStat(&fakestat);
373 out1:
374     AFS_GUNLOCK();
375     afs_maybe_unlock_kernel();
376     return code;
377 }
378
379
380 /* in afs_pioctl.c */
381 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
382                       unsigned long arg);
383
384 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
385 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
386                                unsigned long arg) {
387     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
388
389 }
390 #endif
391
392
393 static int
394 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
395 {
396     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
397     int code;
398
399     AFS_GLOCK();
400     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
401                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
402                vmap->vm_end - vmap->vm_start);
403
404     /* get a validated vcache entry */
405     code = afs_linux_VerifyVCache(vcp, NULL);
406
407     /* Linux's Flushpage implementation doesn't use credp, so optimise
408      * our code to not need to crref() it */
409     osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
410     AFS_GUNLOCK();
411     code = generic_file_mmap(fp, vmap);
412     AFS_GLOCK();
413     if (!code)
414         vcp->f.states |= CMAPPED;
415
416     AFS_GUNLOCK();
417     return code;
418 }
419
420 static int
421 afs_linux_open(struct inode *ip, struct file *fp)
422 {
423     struct vcache *vcp = VTOAFS(ip);
424     cred_t *credp = crref();
425     int code;
426
427     afs_maybe_lock_kernel();
428     AFS_GLOCK();
429     code = afs_open(&vcp, fp->f_flags, credp);
430     AFS_GUNLOCK();
431     afs_maybe_unlock_kernel();
432
433     crfree(credp);
434     return afs_convert_code(code);
435 }
436
437 static int
438 afs_linux_release(struct inode *ip, struct file *fp)
439 {
440     struct vcache *vcp = VTOAFS(ip);
441     cred_t *credp = crref();
442     int code = 0;
443
444     afs_maybe_lock_kernel();
445     AFS_GLOCK();
446     code = afs_close(vcp, fp->f_flags, credp);
447     AFS_GUNLOCK();
448     afs_maybe_unlock_kernel();
449
450     crfree(credp);
451     return afs_convert_code(code);
452 }
453
454 static int
455 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
456 {
457     int code;
458     struct inode *ip = FILE_INODE(fp);
459     cred_t *credp = crref();
460
461     afs_maybe_lock_kernel();
462     AFS_GLOCK();
463     code = afs_fsync(VTOAFS(ip), credp);
464     AFS_GUNLOCK();
465     afs_maybe_unlock_kernel();
466     crfree(credp);
467     return afs_convert_code(code);
468
469 }
470
471
472 static int
473 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
474 {
475     int code = 0;
476     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
477     cred_t *credp = crref();
478     struct AFS_FLOCK flock;
479     
480     /* Convert to a lock format afs_lockctl understands. */
481     memset((char *)&flock, 0, sizeof(flock));
482     flock.l_type = flp->fl_type;
483     flock.l_pid = flp->fl_pid;
484     flock.l_whence = 0;
485     flock.l_start = flp->fl_start;
486     flock.l_len = flp->fl_end - flp->fl_start + 1;
487
488     /* Safe because there are no large files, yet */
489 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
490     if (cmd == F_GETLK64)
491         cmd = F_GETLK;
492     else if (cmd == F_SETLK64)
493         cmd = F_SETLK;
494     else if (cmd == F_SETLKW64)
495         cmd = F_SETLKW;
496 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
497
498     AFS_GLOCK();
499     code = afs_lockctl(vcp, &flock, cmd, credp);
500     AFS_GUNLOCK();
501
502     if ((code == 0 || flp->fl_type == F_UNLCK) && 
503         (cmd == F_SETLK || cmd == F_SETLKW)) {
504         code = afs_posix_lock_file(fp, flp);
505         if (code && flp->fl_type != F_UNLCK) {
506             struct AFS_FLOCK flock2;
507             flock2 = flock;
508             flock2.l_type = F_UNLCK;
509             AFS_GLOCK();
510             afs_lockctl(vcp, &flock2, F_SETLK, credp);
511             AFS_GUNLOCK();
512         }
513     }
514     /* If lockctl says there are no conflicting locks, then also check with the
515      * kernel, as lockctl knows nothing about byte range locks
516      */
517     if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
518         afs_posix_test_lock(fp, flp);
519         /* If we found a lock in the kernel's structure, return it */
520         if (flp->fl_type != F_UNLCK) {
521             crfree(credp);
522             return 0;
523         }
524     }
525     
526     /* Convert flock back to Linux's file_lock */
527     flp->fl_type = flock.l_type;
528     flp->fl_pid = flock.l_pid;
529     flp->fl_start = flock.l_start;
530     flp->fl_end = flock.l_start + flock.l_len - 1;
531
532     crfree(credp);
533     return afs_convert_code(code);
534 }
535
536 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
537 static int
538 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
539     int code = 0;
540     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
541     cred_t *credp = crref();
542     struct AFS_FLOCK flock;
543     /* Convert to a lock format afs_lockctl understands. */
544     memset((char *)&flock, 0, sizeof(flock));
545     flock.l_type = flp->fl_type;
546     flock.l_pid = flp->fl_pid;
547     flock.l_whence = 0;
548     flock.l_start = 0;
549     flock.l_len = OFFSET_MAX;
550
551     /* Safe because there are no large files, yet */
552 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
553     if (cmd == F_GETLK64)
554         cmd = F_GETLK;
555     else if (cmd == F_SETLK64)
556         cmd = F_SETLK;
557     else if (cmd == F_SETLKW64)
558         cmd = F_SETLKW;
559 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
560
561     AFS_GLOCK();
562     code = afs_lockctl(vcp, &flock, cmd, credp);
563     AFS_GUNLOCK();
564
565     if ((code == 0 || flp->fl_type == F_UNLCK) && 
566         (cmd == F_SETLK || cmd == F_SETLKW)) {
567         flp->fl_flags &=~ FL_SLEEP;
568         code = flock_lock_file_wait(fp, flp);
569         if (code && flp->fl_type != F_UNLCK) {
570             struct AFS_FLOCK flock2;
571             flock2 = flock;
572             flock2.l_type = F_UNLCK;
573             AFS_GLOCK();
574             afs_lockctl(vcp, &flock2, F_SETLK, credp);
575             AFS_GUNLOCK();
576         }
577     }
578     /* Convert flock back to Linux's file_lock */
579     flp->fl_type = flock.l_type;
580     flp->fl_pid = flock.l_pid;
581
582     crfree(credp);
583     return afs_convert_code(code);
584 }
585 #endif
586
587 /* afs_linux_flush
588  * essentially the same as afs_fsync() but we need to get the return
589  * code for the sys_close() here, not afs_linux_release(), so call
590  * afs_StoreAllSegments() with AFS_LASTSTORE
591  */
592 static int
593 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
594 afs_linux_flush(struct file *fp, fl_owner_t id)
595 #else
596 afs_linux_flush(struct file *fp)
597 #endif
598 {
599     struct vrequest treq;
600     struct vcache *vcp;
601     cred_t *credp;
602     int code;
603 #if defined(AFS_CACHE_BYPASS)
604     int bypasscache;
605 #endif
606
607     AFS_GLOCK();
608
609     if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
610         AFS_GUNLOCK();
611         return 0;
612     }
613
614     AFS_DISCON_LOCK();
615
616     credp = crref();
617     vcp = VTOAFS(FILE_INODE(fp));
618
619     code = afs_InitReq(&treq, credp);
620     if (code)
621         goto out;
622 #if defined(AFS_CACHE_BYPASS)
623         /* If caching is bypassed for this file, or globally, just return 0 */
624         if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
625                 bypasscache = 1;
626         else {
627                 ObtainReadLock(&vcp->lock);
628                 if(vcp->cachingStates & FCSBypass)
629                         bypasscache = 1;
630                 ReleaseReadLock(&vcp->lock);
631         }
632         if(bypasscache) {
633             /* future proof: don't rely on 0 return from afs_InitReq */
634             code = 0; goto out;
635         }
636 #endif
637
638     ObtainSharedLock(&vcp->lock, 535);
639     if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
640         UpgradeSToWLock(&vcp->lock, 536);
641         if (!AFS_IS_DISCONNECTED) {
642                 code = afs_StoreAllSegments(vcp,
643                                 &treq,
644                                 AFS_SYNC | AFS_LASTSTORE);
645         } else {
646                 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
647         }
648         ConvertWToSLock(&vcp->lock);
649     }
650     code = afs_CheckCode(code, &treq, 54);
651     ReleaseSharedLock(&vcp->lock);
652
653 out:
654     AFS_DISCON_UNLOCK();
655     AFS_GUNLOCK();
656
657     crfree(credp);
658     return afs_convert_code(code);
659 }
660
661 struct file_operations afs_dir_fops = {
662   .read =       generic_read_dir,
663   .readdir =    afs_linux_readdir,
664 #ifdef HAVE_UNLOCKED_IOCTL
665   .unlocked_ioctl = afs_unlocked_xioctl,
666 #else
667   .ioctl =      afs_xioctl,
668 #endif
669 #ifdef HAVE_COMPAT_IOCTL
670   .compat_ioctl = afs_unlocked_xioctl,
671 #endif
672   .open =       afs_linux_open,
673   .release =    afs_linux_release,
674 };
675
676 struct file_operations afs_file_fops = {
677   .read =       afs_linux_read,
678   .write =      afs_linux_write,
679 #ifdef GENERIC_FILE_AIO_READ
680   .aio_read =   generic_file_aio_read,
681   .aio_write =  generic_file_aio_write,
682 #endif
683 #ifdef HAVE_UNLOCKED_IOCTL
684   .unlocked_ioctl = afs_unlocked_xioctl,
685 #else
686   .ioctl =      afs_xioctl,
687 #endif
688 #ifdef HAVE_COMPAT_IOCTL
689   .compat_ioctl = afs_unlocked_xioctl,
690 #endif
691   .mmap =       afs_linux_mmap,
692   .open =       afs_linux_open,
693   .flush =      afs_linux_flush,
694 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
695   .sendfile =   generic_file_sendfile,
696 #endif
697 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
698   .splice_write = generic_file_splice_write,
699   .splice_read = generic_file_splice_read,
700 #endif
701   .release =    afs_linux_release,
702   .fsync =      afs_linux_fsync,
703   .lock =       afs_linux_lock,
704 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
705   .flock =      afs_linux_flock,
706 #endif
707 };
708
709
710 /**********************************************************************
711  * AFS Linux dentry operations
712  **********************************************************************/
713
714 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
715  * that has its mvid (parent dir's fid) pointer set to the wrong directory
716  * due to being mounted in multiple points at once. If so, check_bad_parent()
717  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
718  * dotdotfid and mtpoint fid members.
719  * Parameters:
720  *   dp - dentry to be checked.
721  * Return Values:
722  *   None.
723  * Sideeffects:
724  *   This dentry's vcache's mvid will be set to the correct parent directory's
725  *   fid.
726  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
727  *   to the correct parent and mountpoint fids.
728  */
729
730 static inline void
731 check_bad_parent(struct dentry *dp)
732 {
733     cred_t *credp;
734     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
735     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
736
737     if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) {       /* bad parent */
738         credp = crref();
739
740         /* force a lookup, so vcp->mvid is fixed up */
741         afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
742         if (!avc || vcp != avc) {       /* bad, very bad.. */
743             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
744                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
745                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
746                        ICL_TYPE_POINTER, dp);
747         }
748         if (avc)
749             AFS_RELE(AFSTOV(avc));
750         crfree(credp);
751     }
752
753     return;
754 }
755
756 /* afs_linux_revalidate
757  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
758  */
759 static int
760 afs_linux_revalidate(struct dentry *dp)
761 {
762     struct vattr vattr;
763     struct vcache *vcp = VTOAFS(dp->d_inode);
764     cred_t *credp;
765     int code;
766
767     if (afs_shuttingdown)
768         return EIO;
769
770     afs_maybe_lock_kernel();
771     AFS_GLOCK();
772
773 #ifdef notyet
774     /* Make this a fast path (no crref), since it's called so often. */
775     if (vcp->f.states & CStatd) {
776
777         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
778             check_bad_parent(dp);       /* check and correct mvid */
779
780         AFS_GUNLOCK();
781         unlock_kernel();
782         return 0;
783     }
784 #endif
785
786     /* This avoids the crref when we don't have to do it. Watch for
787      * changes in afs_getattr that don't get replicated here!
788      */
789     if (vcp->f.states & CStatd &&
790         (!afs_fakestat_enable || vcp->mvstat != 1) &&
791         !afs_nfsexporter) {
792         code = afs_CopyOutAttrs(vcp, &vattr);
793     } else {
794         credp = crref();
795         code = afs_getattr(vcp, &vattr, credp);
796         crfree(credp);
797     }
798     if (!code)
799         afs_fill_inode(AFSTOV(vcp), &vattr);
800
801     AFS_GUNLOCK();
802     afs_maybe_unlock_kernel();
803
804     return afs_convert_code(code);
805 }
806
807 static int
808 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
809 {
810         int err = afs_linux_revalidate(dentry);
811         if (!err) {
812                 generic_fillattr(dentry->d_inode, stat);
813 }
814         return err;
815 }
816
817 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
818  * In kernels 2.2.10 and above, we are passed an additional flags var which
819  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
820  * we are advised to follow the entry if it is a link or to make sure that 
821  * it is a directory. But since the kernel itself checks these possibilities
822  * later on, we shouldn't have to do it until later. Perhaps in the future..
823  */
824 static int
825 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
826 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
827 #else
828 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
829 #endif
830 {
831     struct vattr vattr;
832     cred_t *credp = NULL;
833     struct vcache *vcp, *pvcp, *tvc = NULL;
834     int valid;
835     struct afs_fakestat_state fakestate;
836
837     afs_maybe_lock_kernel();
838     AFS_GLOCK();
839     afs_InitFakeStat(&fakestate);
840
841     if (dp->d_inode) {
842
843         vcp = VTOAFS(dp->d_inode);
844         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
845
846         if (vcp == afs_globalVp)
847             goto good_dentry;
848
849         if (vcp->mvstat == 1) {         /* mount point */
850             if (vcp->mvid && (vcp->f.states & CMValid)) {
851                 int tryEvalOnly = 0;
852                 int code = 0;
853                 struct vrequest treq;
854
855                 credp = crref();
856                 code = afs_InitReq(&treq, credp);
857                 if (
858                     (strcmp(dp->d_name.name, ".directory") == 0)) {
859                     tryEvalOnly = 1;
860                 }
861                 if (tryEvalOnly)
862                     code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
863                 else
864                     code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
865                 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
866                     /* a mount point, not yet replaced by its directory */
867                     goto bad_dentry;
868                 }
869             }
870         } else
871             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
872                 check_bad_parent(dp);   /* check and correct mvid */
873
874 #ifdef notdef
875         /* If the last looker changes, we should make sure the current
876          * looker still has permission to examine this file.  This would
877          * always require a crref() which would be "slow".
878          */
879         if (vcp->last_looker != treq.uid) {
880             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
881                 goto bad_dentry;
882
883             vcp->last_looker = treq.uid;
884         }
885 #endif
886
887         /* If the parent's DataVersion has changed or the vnode
888          * is longer valid, we need to do a full lookup.  VerifyVCache
889          * isn't enough since the vnode may have been renamed.
890          */
891
892         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
893
894             credp = crref();
895             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
896             if (!tvc || tvc != vcp)
897                 goto bad_dentry;
898
899             if (afs_getattr(vcp, &vattr, credp))
900                 goto bad_dentry;
901
902             vattr2inode(AFSTOV(vcp), &vattr);
903             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
904         }
905
906         /* should we always update the attributes at this point? */
907         /* unlikely--the vcache entry hasn't changed */
908
909     } else {
910 #ifdef notyet
911         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
912         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
913             goto bad_dentry;
914 #endif
915
916         /* No change in parent's DataVersion so this negative
917          * lookup is still valid.  BUT, if a server is down a
918          * negative lookup can result so there should be a
919          * liftime as well.  For now, always expire.
920          */
921
922         goto bad_dentry;
923     }
924
925   good_dentry:
926     valid = 1;
927
928   done:
929     /* Clean up */
930     if (tvc)
931         afs_PutVCache(tvc);
932     afs_PutFakeStat(&fakestate);
933     AFS_GUNLOCK();
934     if (credp)
935         crfree(credp);
936
937     if (!valid) {
938         shrink_dcache_parent(dp);
939         d_drop(dp);
940     }
941     afs_maybe_unlock_kernel();
942     return valid;
943
944   bad_dentry:
945     if (have_submounts(dp))
946         valid = 1;
947     else 
948         valid = 0;
949     goto done;
950 }
951
952 static void
953 afs_dentry_iput(struct dentry *dp, struct inode *ip)
954 {
955     struct vcache *vcp = VTOAFS(ip);
956
957     AFS_GLOCK();
958     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
959         (void) afs_InactiveVCache(vcp, NULL);
960     }
961     AFS_GUNLOCK();
962     afs_linux_clear_nfsfs_renamed(dp);
963
964     iput(ip);
965 }
966
967 static int
968 afs_dentry_delete(struct dentry *dp)
969 {
970     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
971         return 1;               /* bad inode? */
972
973     return 0;
974 }
975
976 struct dentry_operations afs_dentry_operations = {
977   .d_revalidate =       afs_linux_dentry_revalidate,
978   .d_delete =           afs_dentry_delete,
979   .d_iput =             afs_dentry_iput,
980 };
981
982 /**********************************************************************
983  * AFS Linux inode operations
984  **********************************************************************/
985
986 /* afs_linux_create
987  *
988  * Merely need to set enough of vattr to get us through the create. Note
989  * that the higher level code (open_namei) will take care of any tuncation
990  * explicitly. Exclusive open is also taken care of in open_namei.
991  *
992  * name is in kernel space at this point.
993  */
994 static int
995 #ifdef IOP_CREATE_TAKES_NAMEIDATA
996 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
997                  struct nameidata *nd)
998 #else
999 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1000 #endif
1001 {
1002     struct vattr vattr;
1003     cred_t *credp = crref();
1004     const char *name = dp->d_name.name;
1005     struct vcache *vcp;
1006     int code;
1007
1008     VATTR_NULL(&vattr);
1009     vattr.va_mode = mode;
1010     vattr.va_type = mode & S_IFMT;
1011
1012     afs_maybe_lock_kernel();
1013     AFS_GLOCK();
1014     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1015                       &vcp, credp);
1016
1017     if (!code) {
1018         struct inode *ip = AFSTOV(vcp);
1019
1020         afs_getattr(vcp, &vattr, credp);
1021         afs_fill_inode(ip, &vattr);
1022         insert_inode_hash(ip);
1023         dp->d_op = &afs_dentry_operations;
1024         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1025         d_instantiate(dp, ip);
1026     }
1027     AFS_GUNLOCK();
1028
1029     afs_maybe_unlock_kernel();
1030     crfree(credp);
1031     return afs_convert_code(code);
1032 }
1033
1034 /* afs_linux_lookup */
1035 static struct dentry *
1036 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1037 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1038                  struct nameidata *nd)
1039 #else
1040 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1041 #endif
1042 {
1043     cred_t *credp = crref();
1044     struct vcache *vcp = NULL;
1045     const char *comp = dp->d_name.name;
1046     struct inode *ip = NULL;
1047     struct dentry *newdp = NULL;
1048     int code;
1049
1050     afs_maybe_lock_kernel();
1051     AFS_GLOCK();
1052     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1053     
1054     if (vcp) {
1055         struct vattr vattr;
1056
1057         ip = AFSTOV(vcp);
1058         afs_getattr(vcp, &vattr, credp);
1059         afs_fill_inode(ip, &vattr);
1060         if (hlist_unhashed(&ip->i_hash))
1061             insert_inode_hash(ip);
1062     }
1063     dp->d_op = &afs_dentry_operations;
1064     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1065     AFS_GUNLOCK();
1066
1067     if (ip && S_ISDIR(ip->i_mode)) {
1068         struct dentry *alias;
1069
1070         /* Try to invalidate an existing alias in favor of our new one */
1071         alias = d_find_alias(ip);
1072         /* But not if it's disconnected; then we want d_splice_alias below */
1073         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1074             if (d_invalidate(alias) == 0) {
1075                 dput(alias);
1076             } else {
1077                 iput(ip);
1078                 unlock_kernel();
1079                 crfree(credp);
1080                 return alias;
1081             }
1082         }
1083     }
1084     newdp = d_splice_alias(ip, dp);
1085
1086     afs_maybe_unlock_kernel();
1087     crfree(credp);
1088
1089     /* It's ok for the file to not be found. That's noted by the caller by
1090      * seeing that the dp->d_inode field is NULL.
1091      */
1092     if (!code || code == ENOENT)
1093         return newdp;
1094     else 
1095         return ERR_PTR(afs_convert_code(code));
1096 }
1097
1098 static int
1099 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1100 {
1101     int code;
1102     cred_t *credp = crref();
1103     const char *name = newdp->d_name.name;
1104     struct inode *oldip = olddp->d_inode;
1105
1106     /* If afs_link returned the vnode, we could instantiate the
1107      * dentry. Since it's not, we drop this one and do a new lookup.
1108      */
1109     d_drop(newdp);
1110
1111     AFS_GLOCK();
1112     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1113
1114     AFS_GUNLOCK();
1115     crfree(credp);
1116     return afs_convert_code(code);
1117 }
1118
1119 static int
1120 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1121 {
1122     int code = EBUSY;
1123     cred_t *credp = crref();
1124     const char *name = dp->d_name.name;
1125     struct vcache *tvc = VTOAFS(dp->d_inode);
1126
1127     afs_maybe_lock_kernel();
1128     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1129                                 && !(tvc->f.states & CUnlinked)) {
1130         struct dentry *__dp;
1131         char *__name;
1132
1133         __dp = NULL;
1134         __name = NULL;
1135         do {
1136             dput(__dp);
1137
1138             AFS_GLOCK();
1139             if (__name)
1140                 osi_FreeSmallSpace(__name);
1141             __name = afs_newname();
1142             AFS_GUNLOCK();
1143
1144             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1145                 
1146             if (IS_ERR(__dp))
1147                 goto out;
1148         } while (__dp->d_inode != NULL);
1149
1150         AFS_GLOCK();
1151         code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1152         if (!code) {
1153             tvc->mvid = (void *) __name;
1154             crhold(credp);
1155             if (tvc->uncred) {
1156                 crfree(tvc->uncred);
1157             }
1158             tvc->uncred = credp;
1159             tvc->f.states |= CUnlinked;
1160             afs_linux_set_nfsfs_renamed(dp);
1161         } else {
1162             osi_FreeSmallSpace(__name); 
1163         }
1164         AFS_GUNLOCK();
1165
1166         if (!code) {
1167             __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1168             d_move(dp, __dp);
1169         }
1170         dput(__dp);
1171
1172         goto out;
1173     }
1174
1175     AFS_GLOCK();
1176     code = afs_remove(VTOAFS(dip), (char *)name, credp);
1177     AFS_GUNLOCK();
1178     if (!code)
1179         d_drop(dp);
1180 out:
1181     afs_maybe_unlock_kernel();
1182     crfree(credp);
1183     return afs_convert_code(code);
1184 }
1185
1186
1187 static int
1188 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1189 {
1190     int code;
1191     cred_t *credp = crref();
1192     struct vattr vattr;
1193     const char *name = dp->d_name.name;
1194
1195     /* If afs_symlink returned the vnode, we could instantiate the
1196      * dentry. Since it's not, we drop this one and do a new lookup.
1197      */
1198     d_drop(dp);
1199
1200     VATTR_NULL(&vattr);
1201     AFS_GLOCK();
1202     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1203     AFS_GUNLOCK();
1204     crfree(credp);
1205     return afs_convert_code(code);
1206 }
1207
1208 static int
1209 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1210 {
1211     int code;
1212     cred_t *credp = crref();
1213     struct vcache *tvcp = NULL;
1214     struct vattr vattr;
1215     const char *name = dp->d_name.name;
1216
1217     afs_maybe_lock_kernel();
1218     VATTR_NULL(&vattr);
1219     vattr.va_mask = ATTR_MODE;
1220     vattr.va_mode = mode;
1221     AFS_GLOCK();
1222     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1223
1224     if (tvcp) {
1225         struct inode *ip = AFSTOV(tvcp);
1226
1227         afs_getattr(tvcp, &vattr, credp);
1228         afs_fill_inode(ip, &vattr);
1229
1230         dp->d_op = &afs_dentry_operations;
1231         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1232         d_instantiate(dp, ip);
1233     }
1234     AFS_GUNLOCK();
1235
1236     afs_maybe_unlock_kernel();
1237     crfree(credp);
1238     return afs_convert_code(code);
1239 }
1240
1241 static int
1242 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1243 {
1244     int code;
1245     cred_t *credp = crref();
1246     const char *name = dp->d_name.name;
1247
1248     /* locking kernel conflicts with glock? */
1249
1250     AFS_GLOCK();
1251     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1252     AFS_GUNLOCK();
1253
1254     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1255      * that failed because a directory is not empty. So, we map
1256      * EEXIST to ENOTEMPTY on linux.
1257      */
1258     if (code == EEXIST) {
1259         code = ENOTEMPTY;
1260     }
1261
1262     if (!code) {
1263         d_drop(dp);
1264     }
1265
1266     crfree(credp);
1267     return afs_convert_code(code);
1268 }
1269
1270
1271 static int
1272 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1273                  struct inode *newip, struct dentry *newdp)
1274 {
1275     int code;
1276     cred_t *credp = crref();
1277     const char *oldname = olddp->d_name.name;
1278     const char *newname = newdp->d_name.name;
1279     struct dentry *rehash = NULL;
1280
1281     /* Prevent any new references during rename operation. */
1282     afs_maybe_lock_kernel();
1283
1284     if (!d_unhashed(newdp)) {
1285         d_drop(newdp);
1286         rehash = newdp;
1287     }
1288
1289     if (atomic_read(&olddp->d_count) > 1)
1290         shrink_dcache_parent(olddp);
1291
1292     AFS_GLOCK();
1293     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1294     AFS_GUNLOCK();
1295
1296     if (!code)
1297         olddp->d_time = 0;      /* force to revalidate */
1298
1299     if (rehash)
1300         d_rehash(rehash);
1301
1302     afs_maybe_unlock_kernel();
1303
1304     crfree(credp);
1305     return afs_convert_code(code);
1306 }
1307
1308
1309 /* afs_linux_ireadlink 
1310  * Internal readlink which can return link contents to user or kernel space.
1311  * Note that the buffer is NOT supposed to be null-terminated.
1312  */
1313 static int
1314 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1315 {
1316     int code;
1317     cred_t *credp = crref();
1318     uio_t tuio;
1319     struct iovec iov;
1320
1321     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1322     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1323     crfree(credp);
1324
1325     if (!code)
1326         return maxlen - tuio.uio_resid;
1327     else
1328         return afs_convert_code(code);
1329 }
1330
1331 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1332 /* afs_linux_readlink 
1333  * Fill target (which is in user space) with contents of symlink.
1334  */
1335 static int
1336 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1337 {
1338     int code;
1339     struct inode *ip = dp->d_inode;
1340
1341     AFS_GLOCK();
1342     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1343     AFS_GUNLOCK();
1344     return code;
1345 }
1346
1347
1348 /* afs_linux_follow_link
1349  * a file system dependent link following routine.
1350  */
1351 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1352 {
1353     int code;
1354     char *name;
1355
1356     name = osi_Alloc(PATH_MAX);
1357     if (!name) {
1358         return -EIO;
1359     }
1360
1361     AFS_GLOCK();
1362     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1363     AFS_GUNLOCK();
1364
1365     if (code < 0) {
1366         goto out;
1367     }
1368
1369     name[code] = '\0';
1370     code = vfs_follow_link(nd, name);
1371
1372 out:
1373     osi_Free(name, PATH_MAX);
1374
1375     return code;
1376 }
1377
1378 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1379
1380 #if defined(AFS_CACHE_BYPASS)
1381 #endif /* defined(AFS_CACHE_BYPASS */
1382
1383 /* Populate a page by filling it from the cache file pointed at by cachefp
1384  * (which contains indicated chunk)
1385  * If task is NULL, the page copy occurs syncronously, and the routine
1386  * returns with page still locked. If task is non-NULL, then page copies
1387  * may occur in the background, and the page will be unlocked when it is
1388  * ready for use.
1389  */
1390 static int
1391 afs_linux_read_cache(struct file *cachefp, struct page *page,
1392                      int chunk, struct pagevec *lrupv,
1393                      struct afs_pagecopy_task *task) {
1394     loff_t offset = page_offset(page);
1395     struct page *newpage, *cachepage;
1396     struct address_space *cachemapping;
1397     int pageindex;
1398     int code = 0;
1399
1400     cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1401     newpage = NULL;
1402     cachepage = NULL;
1403
1404     /* From our offset, we now need to work out which page in the disk
1405      * file it corresponds to. This will be fun ... */
1406     pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1407
1408     while (cachepage == NULL) {
1409         cachepage = find_get_page(cachemapping, pageindex);
1410         if (!cachepage) {
1411             if (!newpage)
1412                 newpage = page_cache_alloc_cold(cachemapping);
1413             if (!newpage) {
1414                 code = -ENOMEM;
1415                 goto out;
1416             }
1417
1418             code = add_to_page_cache(newpage, cachemapping,
1419                                      pageindex, GFP_KERNEL);
1420             if (code == 0) {
1421                 cachepage = newpage;
1422                 newpage = NULL;
1423
1424                 page_cache_get(cachepage);
1425                 if (!pagevec_add(lrupv, cachepage))
1426                     __pagevec_lru_add_file(lrupv);
1427
1428             } else {
1429                 page_cache_release(newpage);
1430                 newpage = NULL;
1431                 if (code != -EEXIST)
1432                     goto out;
1433             }
1434         } else {
1435             lock_page(cachepage);
1436         }
1437     }
1438
1439     if (!PageUptodate(cachepage)) {
1440         ClearPageError(cachepage);
1441         code = cachemapping->a_ops->readpage(NULL, cachepage);
1442         if (!code && !task) {
1443             wait_on_page_locked(cachepage);
1444         }
1445     } else {
1446         unlock_page(cachepage);
1447     }
1448
1449     if (!code) {
1450         if (PageUptodate(cachepage)) {
1451             copy_highpage(page, cachepage);
1452             flush_dcache_page(page);
1453             SetPageUptodate(page);
1454
1455             if (task)
1456                 UnlockPage(page);
1457         } else if (task) {
1458             afs_pagecopy_queue_page(task, cachepage, page);
1459         } else {
1460             code = -EIO;
1461         }
1462     }
1463
1464     if (code && task) {
1465         UnlockPage(page);
1466     }
1467
1468 out:
1469     if (cachepage)
1470         page_cache_release(cachepage);
1471
1472     return code;
1473 }
1474
1475 static int inline
1476 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1477 {
1478     loff_t offset = page_offset(pp);
1479     struct inode *ip = FILE_INODE(fp);
1480     struct vcache *avc = VTOAFS(ip);
1481     struct dcache *tdc;
1482     struct file *cacheFp = NULL;
1483     int code;
1484     int dcLocked = 0;
1485     struct pagevec lrupv;
1486
1487     /* Not a UFS cache, don't do anything */
1488     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1489         return 0;
1490
1491     /* Can't do anything if the vcache isn't statd , or if the read
1492      * crosses a chunk boundary.
1493      */
1494     if (!(avc->f.states & CStatd) ||
1495         AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1496         return 0;
1497     }
1498
1499     ObtainWriteLock(&avc->lock, 911);
1500
1501     /* XXX - See if hinting actually makes things faster !!! */
1502
1503     /* See if we have a suitable entry already cached */
1504     tdc = avc->dchint;
1505
1506     if (tdc) {
1507         /* We need to lock xdcache, then dcache, to handle situations where
1508          * the hint is on the free list. However, we can't safely do this
1509          * according to the locking hierarchy. So, use a non blocking lock.
1510          */
1511         ObtainReadLock(&afs_xdcache);
1512         dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1513
1514         if (dcLocked && (tdc->index != NULLIDX)
1515             && !FidCmp(&tdc->f.fid, &avc->f.fid)
1516             && tdc->f.chunk == AFS_CHUNK(offset)
1517             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1518             /* Bonus - the hint was correct */
1519             afs_RefDCache(tdc);
1520         } else {
1521             /* Only destroy the hint if its actually invalid, not if there's
1522              * just been a locking failure */
1523             if (dcLocked) {
1524                 ReleaseReadLock(&tdc->lock);
1525                 avc->dchint = NULL;
1526             }
1527
1528             tdc = NULL;
1529             dcLocked = 0;
1530         }
1531         ReleaseReadLock(&afs_xdcache);
1532     }
1533
1534     /* No hint, or hint is no longer valid - see if we can get something
1535      * directly from the dcache
1536      */
1537     if (!tdc)
1538         tdc = afs_FindDCache(avc, offset);
1539
1540     if (!tdc) {
1541         ReleaseWriteLock(&avc->lock);
1542         return 0;
1543     }
1544
1545     if (!dcLocked)
1546         ObtainReadLock(&tdc->lock);
1547
1548     /* Is the dcache we've been given currently up to date */
1549     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1550         (tdc->dflags & DFFetching)) {
1551         ReleaseWriteLock(&avc->lock);
1552         ReleaseReadLock(&tdc->lock);
1553         afs_PutDCache(tdc);
1554         return 0;
1555     }
1556
1557     /* Update our hint for future abuse */
1558     avc->dchint = tdc;
1559
1560     /* Okay, so we've now got a cache file that is up to date */
1561
1562     /* XXX - I suspect we should be locking the inodes before we use them! */
1563     AFS_GUNLOCK();
1564     cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1565     pagevec_init(&lrupv, 0);
1566
1567     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1568
1569     if (pagevec_count(&lrupv))
1570        __pagevec_lru_add_file(&lrupv);
1571
1572     filp_close(cacheFp, NULL);
1573     AFS_GLOCK();
1574
1575     ReleaseReadLock(&tdc->lock);
1576     ReleaseWriteLock(&avc->lock);
1577     afs_PutDCache(tdc);
1578
1579     *codep = code;
1580     return 1;
1581 }
1582
1583 /* afs_linux_readpage
1584  *
1585  * This function is split into two, because prepare_write/begin_write
1586  * require a readpage call which doesn't unlock the resulting page upon
1587  * success.
1588  */
1589 static int
1590 afs_linux_fillpage(struct file *fp, struct page *pp)
1591 {
1592     afs_int32 code;
1593     char *address;
1594     uio_t *auio;
1595     struct iovec *iovecp;
1596     struct inode *ip = FILE_INODE(fp);
1597     afs_int32 cnt = page_count(pp);
1598     struct vcache *avc = VTOAFS(ip);
1599     afs_offs_t offset = page_offset(pp);
1600     cred_t *credp;
1601
1602     AFS_GLOCK();
1603     if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1604         AFS_GUNLOCK();
1605         return code;
1606     }
1607     AFS_GUNLOCK();
1608
1609     credp = crref();
1610     address = kmap(pp);
1611     ClearPageError(pp);
1612
1613     auio = osi_Alloc(sizeof(uio_t));
1614     iovecp = osi_Alloc(sizeof(struct iovec));
1615
1616     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1617               AFS_UIOSYS);
1618
1619     afs_maybe_lock_kernel();
1620     AFS_GLOCK();
1621     AFS_DISCON_LOCK();
1622     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1623                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1624                99999);  /* not a possible code value */
1625
1626     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1627         
1628     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1629                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1630                code);
1631     AFS_DISCON_UNLOCK();
1632     AFS_GUNLOCK();
1633     afs_maybe_unlock_kernel();
1634     if (!code) {
1635         /* XXX valid for no-cache also?  Check last bits of files... :)
1636          * Cognate code goes in afs_NoCacheFetchProc.  */
1637         if (auio->uio_resid)    /* zero remainder of page */
1638              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1639                     auio->uio_resid);
1640
1641         flush_dcache_page(pp);
1642         SetPageUptodate(pp);
1643     } /* !code */
1644
1645     kunmap(pp);
1646
1647     osi_Free(auio, sizeof(uio_t));
1648     osi_Free(iovecp, sizeof(struct iovec));
1649
1650     crfree(credp);
1651     return afs_convert_code(code);
1652 }
1653
1654 static int
1655 afs_linux_prefetch(struct file *fp, struct page *pp)
1656 {
1657     int code = 0;
1658     struct vcache *avc = VTOAFS(FILE_INODE(fp));
1659     afs_offs_t offset = page_offset(pp);
1660
1661     if (AFS_CHUNKOFFSET(offset) == 0) {
1662         struct dcache *tdc;
1663         struct vrequest treq;
1664         cred_t *credp;
1665
1666         credp = crref();
1667         AFS_GLOCK();
1668         code = afs_InitReq(&treq, credp);
1669         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1670             tdc = afs_FindDCache(avc, offset);
1671             if (tdc) {
1672                 if (!(tdc->mflags & DFNextStarted))
1673                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1674                     afs_PutDCache(tdc);
1675             }
1676             ReleaseWriteLock(&avc->lock);
1677         }
1678         AFS_GUNLOCK();
1679         crfree(credp);
1680     }
1681     return afs_convert_code(code);
1682
1683 }
1684
1685 #if defined(AFS_CACHE_BYPASS)
1686
1687 static int
1688 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1689                            struct list_head *page_list, unsigned num_pages)
1690 {
1691     afs_int32 page_ix;
1692     uio_t *auio;
1693     afs_offs_t offset;
1694     struct iovec* iovecp;
1695     struct nocache_read_request *ancr;
1696     struct page *pp, *ppt;
1697     struct pagevec lrupv;
1698     afs_int32 code = 0;
1699
1700     cred_t *credp;
1701     struct inode *ip = FILE_INODE(fp);
1702     struct vcache *avc = VTOAFS(ip);
1703     afs_int32 base_index = 0;
1704     afs_int32 page_count = 0;
1705     afs_int32 isize;
1706
1707     /* background thread must free: iovecp, auio, ancr */
1708     iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1709
1710     auio = osi_Alloc(sizeof(uio_t));
1711     auio->uio_iov = iovecp;
1712     auio->uio_iovcnt = num_pages;
1713     auio->uio_flag = UIO_READ;
1714     auio->uio_seg = AFS_UIOSYS;
1715     auio->uio_resid = num_pages * PAGE_SIZE;
1716
1717     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1718     ancr->auio = auio;
1719     ancr->offset = auio->uio_offset;
1720     ancr->length = auio->uio_resid;
1721
1722     pagevec_init(&lrupv, 0);
1723
1724     for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1725
1726         if(list_empty(page_list))
1727             break;
1728
1729         pp = list_entry(page_list->prev, struct page, lru);
1730         /* If we allocate a page and don't remove it from page_list,
1731          * the page cache gets upset. */
1732         list_del(&pp->lru);
1733         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1734         if(pp->index > isize) {
1735             if(PageLocked(pp))
1736                 UnlockPage(pp);
1737             continue;
1738         }
1739
1740         if(page_ix == 0) {
1741             offset = page_offset(pp);
1742             auio->uio_offset = offset;
1743             base_index = pp->index;
1744         }
1745         iovecp[page_ix].iov_len = PAGE_SIZE;
1746         code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1747         if(base_index != pp->index) {
1748             if(PageLocked(pp))
1749                                  UnlockPage(pp);
1750             page_cache_release(pp);
1751             iovecp[page_ix].iov_base = (void *) 0;
1752             base_index++;
1753             continue;
1754         }
1755         base_index++;
1756         if(code) {
1757             if(PageLocked(pp))
1758                 UnlockPage(pp);
1759             page_cache_release(pp);
1760             iovecp[page_ix].iov_base = (void *) 0;
1761         } else {
1762             page_count++;
1763             if(!PageLocked(pp)) {
1764                 LockPage(pp);
1765             }
1766
1767             /* save the page for background map */
1768             iovecp[page_ix].iov_base = (void*) pp;
1769
1770             /* and put it on the LRU cache */
1771             if (!pagevec_add(&lrupv, pp))
1772                 __pagevec_lru_add(&lrupv);
1773         }
1774     }
1775
1776     /* If there were useful pages in the page list, make sure all pages
1777      * are in the LRU cache, then schedule the read */
1778     if(page_count) {
1779         pagevec_lru_add(&lrupv);
1780         credp = crref();
1781         code = afs_ReadNoCache(avc, ancr, credp);
1782         crfree(credp);
1783     } else {
1784         /* If there is nothing for the background thread to handle,
1785          * it won't be freeing the things that we never gave it */
1786         osi_Free(iovecp, num_pages * sizeof(struct iovec));
1787         osi_Free(auio, sizeof(uio_t));
1788         osi_Free(ancr, sizeof(struct nocache_read_request));
1789     }
1790     /* we do not flush, release, or unmap pages--that will be
1791      * done for us by the background thread as each page comes in
1792      * from the fileserver */
1793 out:
1794     return afs_convert_code(code);
1795 }
1796
1797
1798 static int
1799 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1800 {
1801     cred_t *credp = NULL;
1802     uio_t *auio;
1803     struct iovec *iovecp;
1804     struct nocache_read_request *ancr;
1805     afs_int32 isize;
1806
1807     ClearPageError(pp);
1808
1809     /* receiver frees */
1810     auio = osi_Alloc(sizeof(uio_t));
1811     iovecp = osi_Alloc(sizeof(struct iovec));
1812
1813     /* address can be NULL, because we overwrite it with 'pp', below */
1814     setup_uio(auio, iovecp, NULL, page_offset(pp),
1815               PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1816
1817     /* save the page for background map */
1818     /* XXX - Shouldn't we get a reference count here? */
1819     auio->uio_iov->iov_base = (void*) pp;
1820     /* the background thread will free this */
1821     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1822     ancr->auio = auio;
1823     ancr->offset = offset;
1824     ancr->length = PAGE_SIZE;
1825
1826     credp = crref();
1827     afs_maybe_lock_kernel();
1828     code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1829     afs_maybe_unlock_kernel();
1830     crfree(credp);
1831
1832     return afs_convert_code(code);
1833 }
1834
1835 static inline int
1836 afs_linux_can_bypass(struct inode *ip) {
1837     switch(cache_bypass_strategy) {
1838         case NEVER_BYPASS_CACHE:
1839             return 0;
1840         case ALWAYS_BYPASS_CACHE:
1841             return 1;
1842         case LARGE_FILES_BYPASS_CACHE:
1843             if(i_size_read(ip) > cache_bypass_threshold)
1844                 return 1;
1845         default:
1846      }
1847      return 0;
1848 }
1849
1850 /* Check if a file is permitted to bypass the cache by policy, and modify
1851  * the cache bypass state recorded for that file */
1852
1853 static inline int
1854 afs_linux_bypass_check(struct inode *ip) {
1855     struct cred* credp;
1856
1857     int bypass = afs_linux_can_bypass(ip);
1858
1859     credp = crref();
1860     trydo_cache_transition(VTOAFS(ip)), credp, bypass);
1861     crfree(credp);
1862
1863     return bypass;
1864 }
1865
1866 #else
1867 static inline int
1868 afs_linux_bypass_check(struct inode *ip) {
1869     return 0;
1870 }
1871 static inline int
1872 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1873     return 0;
1874 }
1875 static inline int
1876 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1877                     struct list_head *page_list, unsigned int num_pages) {
1878     return 0;
1879 }
1880 #endif
1881
1882 static int
1883 afs_linux_readpage(struct file *fp, struct page *pp)
1884 {
1885     int code;
1886
1887     if (afs_linux_bypass_check(FILE_INODE(fp))) {
1888         code = afs_linux_bypass_readpage(fp, pp);
1889     } else {
1890         code = afs_linux_fillpage(fp, pp);
1891         if (!code)
1892             code = afs_linux_prefetch(fp, pp);
1893         UnlockPage(pp);
1894     }
1895
1896     return code;
1897 }
1898
1899 /* Readpages reads a number of pages for a particular file. We use
1900  * this to optimise the reading, by limiting the number of times upon which
1901  * we have to lookup, lock and open vcaches and dcaches
1902  */
1903
1904 static int
1905 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1906                     struct list_head *page_list, unsigned int num_pages)
1907 {
1908     struct inode *inode = mapping->host;
1909     struct vcache *avc = VTOAFS(inode);
1910     struct dcache *tdc;
1911     struct file *cacheFp = NULL;
1912     int code;
1913     unsigned int page_idx;
1914     loff_t offset;
1915     struct pagevec lrupv;
1916     struct afs_pagecopy_task *task;
1917
1918     if (afs_linux_bypass_check(inode))
1919         return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1920
1921     AFS_GLOCK();
1922     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1923         AFS_GUNLOCK();
1924         return code;
1925     }
1926
1927     ObtainWriteLock(&avc->lock, 912);
1928     AFS_GUNLOCK();
1929
1930     task = afs_pagecopy_init_task();
1931
1932     tdc = NULL;
1933     pagevec_init(&lrupv, 0);
1934     for (page_idx = 0; page_idx < num_pages; page_idx++) {
1935         struct page *page = list_entry(page_list->prev, struct page, lru);
1936         list_del(&page->lru);
1937         offset = page_offset(page);
1938
1939         if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1940             AFS_GLOCK();
1941             ReleaseReadLock(&tdc->lock);
1942             afs_PutDCache(tdc);
1943             AFS_GUNLOCK();
1944             tdc = NULL;
1945             if (cacheFp)
1946                 filp_close(cacheFp, NULL);
1947         }
1948
1949         if (!tdc) {
1950             AFS_GLOCK();
1951             if ((tdc = afs_FindDCache(avc, offset))) {
1952                 ObtainReadLock(&tdc->lock);
1953                 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1954                     (tdc->dflags & DFFetching)) {
1955                     ReleaseReadLock(&tdc->lock);
1956                     afs_PutDCache(tdc);
1957                     tdc = NULL;
1958                 }
1959             }
1960             AFS_GUNLOCK();
1961             if (tdc)
1962                 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1963         }
1964
1965         if (tdc && !add_to_page_cache(page, mapping, page->index,
1966                                       GFP_KERNEL)) {
1967             page_cache_get(page);
1968             if (!pagevec_add(&lrupv, page))
1969                 __pagevec_lru_add_file(&lrupv);
1970
1971             afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
1972         }
1973         page_cache_release(page);
1974     }
1975     if (pagevec_count(&lrupv))
1976        __pagevec_lru_add_file(&lrupv);
1977
1978     if (tdc)
1979         filp_close(cacheFp, NULL);
1980
1981     afs_pagecopy_put_task(task);
1982
1983     AFS_GLOCK();
1984     if (tdc) {
1985         ReleaseReadLock(&tdc->lock);
1986         afs_PutDCache(tdc);
1987     }
1988
1989     ReleaseWriteLock(&avc->lock);
1990     AFS_GUNLOCK();
1991     return 0;
1992 }
1993
1994 static int
1995 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1996                          unsigned long offset, unsigned int count)
1997 {
1998     struct vcache *vcp = VTOAFS(ip);
1999     char *buffer;
2000     afs_offs_t base;
2001     int code = 0;
2002     cred_t *credp;
2003     uio_t tuio;
2004     struct iovec iovec;
2005     int f_flags = 0;
2006
2007     buffer = kmap(pp) + offset;
2008     base = page_offset(pp) + offset;
2009
2010     credp = crref();
2011     afs_maybe_lock_kernel();
2012     AFS_GLOCK();
2013     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2014                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2015                ICL_TYPE_INT32, 99999);
2016
2017     ObtainWriteLock(&vcp->lock, 532);
2018     if (vcp->f.states & CPageWrite) {
2019         ReleaseWriteLock(&vcp->lock);
2020         AFS_GUNLOCK();
2021         afs_maybe_unlock_kernel();
2022         crfree(credp);
2023         kunmap(pp);
2024         return AOP_WRITEPAGE_ACTIVATE;
2025     }
2026     vcp->f.states |= CPageWrite;
2027     ReleaseWriteLock(&vcp->lock);
2028
2029     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2030
2031     code = afs_write(vcp, &tuio, f_flags, credp, 0);
2032
2033     i_size_write(ip, vcp->f.m.Length);
2034     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2035
2036     ObtainWriteLock(&vcp->lock, 533);
2037     if (!code) {
2038         struct vrequest treq;
2039
2040         if (!afs_InitReq(&treq, credp))
2041             code = afs_DoPartialWrite(vcp, &treq);
2042     }
2043     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2044
2045     vcp->f.states &= ~CPageWrite;
2046     ReleaseWriteLock(&vcp->lock);
2047
2048     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2049                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2050                ICL_TYPE_INT32, code);
2051
2052     AFS_GUNLOCK();
2053     afs_maybe_unlock_kernel();
2054     crfree(credp);
2055     kunmap(pp);
2056
2057     return code;
2058 }
2059
2060
2061 static int
2062 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2063 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2064 #else
2065 afs_linux_writepage(struct page *pp)
2066 #endif
2067 {
2068     struct address_space *mapping = pp->mapping;
2069     struct inode *inode;
2070     unsigned int to = PAGE_CACHE_SIZE;
2071     loff_t isize;
2072     int status = 0;
2073
2074     if (PageReclaim(pp)) {
2075         return AOP_WRITEPAGE_ACTIVATE;
2076         /* XXX - Do we need to redirty the page here? */
2077     }
2078
2079     page_cache_get(pp);
2080
2081     inode = (struct inode *)mapping->host;
2082     isize = i_size_read(inode);
2083
2084     /* Don't defeat an earlier truncate */
2085     if (page_offset(pp) > isize)
2086         goto done;
2087
2088     /* If this is the final page, then just write the number of bytes that
2089      * are actually in it */
2090     if ((isize - page_offset(pp)) < to )
2091         to = isize - page_offset(pp);
2092
2093     status = afs_linux_writepage_sync(inode, pp, 0, to);
2094
2095 done:
2096     SetPageUptodate(pp);
2097     if ( status != AOP_WRITEPAGE_ACTIVATE ) {
2098         /* XXX - do we need to redirty the page here? */
2099         UnlockPage(pp);
2100     }
2101
2102     page_cache_release(pp);
2103
2104     if (status == to)
2105         return 0;
2106     else
2107         return status;
2108 }
2109
2110 /* afs_linux_permission
2111  * Check access rights - returns error if can't check or permission denied.
2112  */
2113 static int
2114 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2115 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2116 #else
2117 afs_linux_permission(struct inode *ip, int mode)
2118 #endif
2119 {
2120     int code;
2121     cred_t *credp = crref();
2122     int tmp = 0;
2123
2124     AFS_GLOCK();
2125     if (mode & MAY_EXEC)
2126         tmp |= VEXEC;
2127     if (mode & MAY_READ)
2128         tmp |= VREAD;
2129     if (mode & MAY_WRITE)
2130         tmp |= VWRITE;
2131     code = afs_access(VTOAFS(ip), tmp, credp);
2132
2133     AFS_GUNLOCK();
2134     crfree(credp);
2135     return afs_convert_code(code);
2136 }
2137
2138 #if !defined(HAVE_WRITE_BEGIN)
2139 static int
2140 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2141                        unsigned to)
2142 {
2143     int code;
2144
2145     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2146                                     offset, to - offset);
2147
2148     return code;
2149 }
2150
2151 static int
2152 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2153                         unsigned to)
2154 {
2155     return 0;
2156 }
2157 #else
2158
2159 static int
2160 afs_linux_write_end(struct file *file, struct address_space *mapping,
2161                                 loff_t pos, unsigned len, unsigned copied,
2162                                 struct page *page, void *fsdata)
2163 {
2164     int code;
2165     unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2166
2167     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2168                                     from, copied);
2169     unlock_page(page);
2170     page_cache_release(page);
2171     return code;
2172 }
2173
2174 static int
2175 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2176                                 loff_t pos, unsigned len, unsigned flags,
2177                                 struct page **pagep, void **fsdata)
2178 {
2179     struct page *page;
2180     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2181     page = grab_cache_page_write_begin(mapping, index, flags);
2182     *pagep = page;
2183
2184     return 0;
2185 }
2186 #endif
2187
2188
2189 static struct inode_operations afs_file_iops = {
2190   .permission =         afs_linux_permission,
2191   .getattr =            afs_linux_getattr,
2192   .setattr =            afs_notify_change,
2193 };
2194
2195 static struct address_space_operations afs_file_aops = {
2196   .readpage =           afs_linux_readpage,
2197   .readpages =          afs_linux_readpages,
2198   .writepage =          afs_linux_writepage,
2199 #if defined (HAVE_WRITE_BEGIN)
2200   .write_begin =        afs_linux_write_begin,
2201   .write_end =          afs_linux_write_end,
2202 #else
2203   .commit_write =       afs_linux_commit_write,
2204   .prepare_write =      afs_linux_prepare_write,
2205 #endif
2206 };
2207
2208
2209 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2210  * by what sort of operation is allowed.....
2211  */
2212
2213 static struct inode_operations afs_dir_iops = {
2214   .setattr =            afs_notify_change,
2215   .create =             afs_linux_create,
2216   .lookup =             afs_linux_lookup,
2217   .link =               afs_linux_link,
2218   .unlink =             afs_linux_unlink,
2219   .symlink =            afs_linux_symlink,
2220   .mkdir =              afs_linux_mkdir,
2221   .rmdir =              afs_linux_rmdir,
2222   .rename =             afs_linux_rename,
2223   .getattr =            afs_linux_getattr,
2224   .permission =         afs_linux_permission,
2225 };
2226
2227 /* We really need a separate symlink set of ops, since do_follow_link()
2228  * determines if it _is_ a link by checking if the follow_link op is set.
2229  */
2230 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2231 static int
2232 afs_symlink_filler(struct file *file, struct page *page)
2233 {
2234     struct inode *ip = (struct inode *)page->mapping->host;
2235     char *p = (char *)kmap(page);
2236     int code;
2237
2238     afs_maybe_lock_kernel();
2239     AFS_GLOCK();
2240     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2241     AFS_GUNLOCK();
2242
2243     if (code < 0)
2244         goto fail;
2245     p[code] = '\0';             /* null terminate? */
2246     afs_maybe_unlock_kernel();
2247
2248     SetPageUptodate(page);
2249     kunmap(page);
2250     UnlockPage(page);
2251     return 0;
2252
2253   fail:
2254     afs_maybe_unlock_kernel();
2255
2256     SetPageError(page);
2257     kunmap(page);
2258     UnlockPage(page);
2259     return code;
2260 }
2261
2262 static struct address_space_operations afs_symlink_aops = {
2263   .readpage =   afs_symlink_filler
2264 };
2265 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2266
2267 static struct inode_operations afs_symlink_iops = {
2268 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2269   .readlink =           page_readlink,
2270 # if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2271   .follow_link =        page_follow_link,
2272 # else
2273   .follow_link =        page_follow_link_light,
2274   .put_link =           page_put_link,
2275 # endif
2276 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2277   .readlink =           afs_linux_readlink,
2278   .follow_link =        afs_linux_follow_link,
2279 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2280   .setattr =            afs_notify_change,
2281 };
2282
2283 void
2284 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2285 {
2286         
2287     if (vattr)
2288         vattr2inode(ip, vattr);
2289
2290     ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2291 /* Reset ops if symlink or directory. */
2292     if (S_ISREG(ip->i_mode)) {
2293         ip->i_op = &afs_file_iops;
2294         ip->i_fop = &afs_file_fops;
2295         ip->i_data.a_ops = &afs_file_aops;
2296
2297     } else if (S_ISDIR(ip->i_mode)) {
2298         ip->i_op = &afs_dir_iops;
2299         ip->i_fop = &afs_dir_fops;
2300
2301     } else if (S_ISLNK(ip->i_mode)) {
2302         ip->i_op = &afs_symlink_iops;
2303 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2304         ip->i_data.a_ops = &afs_symlink_aops;
2305         ip->i_mapping = &ip->i_data;
2306 #endif
2307     }
2308
2309 }