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