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