780c8efd3c0056827ff6bc636f4e371f6b24f5cc
[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 = 0;
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;
622         goto out;
623     }
624 #endif
625
626     ObtainSharedLock(&vcp->lock, 535);
627     if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
628         UpgradeSToWLock(&vcp->lock, 536);
629         if (!AFS_IS_DISCONNECTED) {
630                 code = afs_StoreAllSegments(vcp,
631                                 &treq,
632                                 AFS_SYNC | AFS_LASTSTORE);
633         } else {
634                 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
635         }
636         ConvertWToSLock(&vcp->lock);
637     }
638     code = afs_CheckCode(code, &treq, 54);
639     ReleaseSharedLock(&vcp->lock);
640
641 out:
642     AFS_DISCON_UNLOCK();
643     AFS_GUNLOCK();
644
645     crfree(credp);
646     return afs_convert_code(code);
647 }
648
649 struct file_operations afs_dir_fops = {
650   .read =       generic_read_dir,
651   .readdir =    afs_linux_readdir,
652 #ifdef HAVE_UNLOCKED_IOCTL
653   .unlocked_ioctl = afs_unlocked_xioctl,
654 #else
655   .ioctl =      afs_xioctl,
656 #endif
657 #ifdef HAVE_COMPAT_IOCTL
658   .compat_ioctl = afs_unlocked_xioctl,
659 #endif
660   .open =       afs_linux_open,
661   .release =    afs_linux_release,
662 };
663
664 struct file_operations afs_file_fops = {
665   .read =       afs_linux_read,
666   .write =      afs_linux_write,
667 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
668   .aio_read =   generic_file_aio_read,
669   .aio_write =  generic_file_aio_write,
670 #endif
671 #ifdef HAVE_UNLOCKED_IOCTL
672   .unlocked_ioctl = afs_unlocked_xioctl,
673 #else
674   .ioctl =      afs_xioctl,
675 #endif
676 #ifdef HAVE_COMPAT_IOCTL
677   .compat_ioctl = afs_unlocked_xioctl,
678 #endif
679   .mmap =       afs_linux_mmap,
680   .open =       afs_linux_open,
681   .flush =      afs_linux_flush,
682 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
683   .sendfile =   generic_file_sendfile,
684 #endif
685 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
686   .splice_write = generic_file_splice_write,
687   .splice_read = generic_file_splice_read,
688 #endif
689   .release =    afs_linux_release,
690   .fsync =      afs_linux_fsync,
691   .lock =       afs_linux_lock,
692 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
693   .flock =      afs_linux_flock,
694 #endif
695 };
696
697
698 /**********************************************************************
699  * AFS Linux dentry operations
700  **********************************************************************/
701
702 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
703  * that has its mvid (parent dir's fid) pointer set to the wrong directory
704  * due to being mounted in multiple points at once. If so, check_bad_parent()
705  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
706  * dotdotfid and mtpoint fid members.
707  * Parameters:
708  *   dp - dentry to be checked.
709  * Return Values:
710  *   None.
711  * Sideeffects:
712  *   This dentry's vcache's mvid will be set to the correct parent directory's
713  *   fid.
714  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
715  *   to the correct parent and mountpoint fids.
716  */
717
718 static inline void
719 check_bad_parent(struct dentry *dp)
720 {
721     cred_t *credp;
722     struct dentry *parent;
723     struct vcache *vcp, *pvc, *avc = NULL;
724
725     vcp = VTOAFS(dp->d_inode);
726     parent = dget_parent(dp);
727     pvc = VTOAFS(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     dput(parent);
746
747     return;
748 }
749
750 /* afs_linux_revalidate
751  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
752  */
753 static int
754 afs_linux_revalidate(struct dentry *dp)
755 {
756     struct vattr vattr;
757     struct vcache *vcp = VTOAFS(dp->d_inode);
758     cred_t *credp;
759     int code;
760
761     if (afs_shuttingdown)
762         return EIO;
763
764     AFS_GLOCK();
765
766 #ifdef notyet
767     /* Make this a fast path (no crref), since it's called so often. */
768     if (vcp->f.states & CStatd) {
769
770         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
771             check_bad_parent(dp);       /* check and correct mvid */
772
773         AFS_GUNLOCK();
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         (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
785         code = afs_CopyOutAttrs(vcp, &vattr);
786     } else {
787         credp = crref();
788         code = afs_getattr(vcp, &vattr, credp);
789         crfree(credp);
790     }
791     if (!code)
792         afs_fill_inode(AFSTOV(vcp), &vattr);
793
794     AFS_GUNLOCK();
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     struct dentry *parent;
827     int valid;
828     struct afs_fakestat_state fakestate;
829
830     AFS_GLOCK();
831     afs_InitFakeStat(&fakestate);
832
833     if (dp->d_inode) {
834         vcp = VTOAFS(dp->d_inode);
835
836         if (vcp == afs_globalVp)
837             goto good_dentry;
838
839         if (vcp->mvstat == 1) {         /* mount point */
840             if (vcp->mvid && (vcp->f.states & CMValid)) {
841                 int tryEvalOnly = 0;
842                 int code = 0;
843                 struct vrequest treq;
844
845                 credp = crref();
846                 code = afs_InitReq(&treq, credp);
847                 if (
848                     (strcmp(dp->d_name.name, ".directory") == 0)) {
849                     tryEvalOnly = 1;
850                 }
851                 if (tryEvalOnly)
852                     code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
853                 else
854                     code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
855                 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
856                     /* a mount point, not yet replaced by its directory */
857                     goto bad_dentry;
858                 }
859             }
860         } else
861             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
862                 check_bad_parent(dp);   /* check and correct mvid */
863
864 #ifdef notdef
865         /* If the last looker changes, we should make sure the current
866          * looker still has permission to examine this file.  This would
867          * always require a crref() which would be "slow".
868          */
869         if (vcp->last_looker != treq.uid) {
870             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
871                 goto bad_dentry;
872
873             vcp->last_looker = treq.uid;
874         }
875 #endif
876
877         parent = dget_parent(dp);
878         pvcp = VTOAFS(parent->d_inode);
879
880         /* If the parent's DataVersion has changed or the vnode
881          * is longer valid, we need to do a full lookup.  VerifyVCache
882          * isn't enough since the vnode may have been renamed.
883          */
884
885         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
886
887             credp = crref();
888             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
889             if (!tvc || tvc != vcp) {
890                 dput(parent);
891                 goto bad_dentry;
892             }
893
894             if (afs_getattr(vcp, &vattr, credp)) {
895                 dput(parent);
896                 goto bad_dentry;
897             }
898
899             vattr2inode(AFSTOV(vcp), &vattr);
900             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
901         }
902
903         /* should we always update the attributes at this point? */
904         /* unlikely--the vcache entry hasn't changed */
905
906         dput(parent);
907     } else {
908 #ifdef notyet
909         /* If this code is ever enabled, we should use dget_parent to handle
910          * getting the parent, and dput() to dispose of it. See above for an
911          * example ... */
912         pvcp = VTOAFS(dp->d_parent->d_inode);
913         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
914             goto bad_dentry;
915 #endif
916
917         /* No change in parent's DataVersion so this negative
918          * lookup is still valid.  BUT, if a server is down a
919          * negative lookup can result so there should be a
920          * liftime as well.  For now, always expire.
921          */
922
923         goto bad_dentry;
924     }
925
926   good_dentry:
927     valid = 1;
928
929   done:
930     /* Clean up */
931     if (tvc)
932         afs_PutVCache(tvc);
933     afs_PutFakeStat(&fakestate);
934     AFS_GUNLOCK();
935     if (credp)
936         crfree(credp);
937
938     if (!valid) {
939         shrink_dcache_parent(dp);
940         d_drop(dp);
941     }
942     return valid;
943
944   bad_dentry:
945     if (have_submounts(dp))
946         valid = 1;
947     else 
948         valid = 0;
949     goto done;
950 }
951
952 static void
953 afs_dentry_iput(struct dentry *dp, struct inode *ip)
954 {
955     struct vcache *vcp = VTOAFS(ip);
956
957     AFS_GLOCK();
958     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
959         (void) afs_InactiveVCache(vcp, NULL);
960     }
961     AFS_GUNLOCK();
962     afs_linux_clear_nfsfs_renamed(dp);
963
964     iput(ip);
965 }
966
967 static int
968 afs_dentry_delete(struct dentry *dp)
969 {
970     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
971         return 1;               /* bad inode? */
972
973     return 0;
974 }
975
976 struct dentry_operations afs_dentry_operations = {
977   .d_revalidate =       afs_linux_dentry_revalidate,
978   .d_delete =           afs_dentry_delete,
979   .d_iput =             afs_dentry_iput,
980 };
981
982 /**********************************************************************
983  * AFS Linux inode operations
984  **********************************************************************/
985
986 /* afs_linux_create
987  *
988  * Merely need to set enough of vattr to get us through the create. Note
989  * that the higher level code (open_namei) will take care of any tuncation
990  * explicitly. Exclusive open is also taken care of in open_namei.
991  *
992  * name is in kernel space at this point.
993  */
994 static int
995 #ifdef IOP_CREATE_TAKES_NAMEIDATA
996 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
997                  struct nameidata *nd)
998 #else
999 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1000 #endif
1001 {
1002     struct vattr vattr;
1003     cred_t *credp = crref();
1004     const char *name = dp->d_name.name;
1005     struct vcache *vcp;
1006     int code;
1007
1008     VATTR_NULL(&vattr);
1009     vattr.va_mode = mode;
1010     vattr.va_type = mode & S_IFMT;
1011
1012     AFS_GLOCK();
1013     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1014                       &vcp, credp);
1015
1016     if (!code) {
1017         struct inode *ip = AFSTOV(vcp);
1018
1019         afs_getattr(vcp, &vattr, credp);
1020         afs_fill_inode(ip, &vattr);
1021         insert_inode_hash(ip);
1022         dp->d_op = &afs_dentry_operations;
1023         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1024         d_instantiate(dp, ip);
1025     }
1026     AFS_GUNLOCK();
1027
1028     crfree(credp);
1029     return afs_convert_code(code);
1030 }
1031
1032 /* afs_linux_lookup */
1033 static struct dentry *
1034 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1035 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1036                  struct nameidata *nd)
1037 #else
1038 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1039 #endif
1040 {
1041     cred_t *credp = crref();
1042     struct vcache *vcp = NULL;
1043     const char *comp = dp->d_name.name;
1044     struct inode *ip = NULL;
1045     struct dentry *newdp = NULL;
1046     int code;
1047
1048     AFS_GLOCK();
1049     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1050     
1051     if (vcp) {
1052         struct vattr vattr;
1053
1054         ip = AFSTOV(vcp);
1055         afs_getattr(vcp, &vattr, credp);
1056         afs_fill_inode(ip, &vattr);
1057         if (hlist_unhashed(&ip->i_hash))
1058             insert_inode_hash(ip);
1059     }
1060     dp->d_op = &afs_dentry_operations;
1061     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1062     AFS_GUNLOCK();
1063
1064     if (ip && S_ISDIR(ip->i_mode)) {
1065         struct dentry *alias;
1066
1067         /* Try to invalidate an existing alias in favor of our new one */
1068         alias = d_find_alias(ip);
1069         /* But not if it's disconnected; then we want d_splice_alias below */
1070         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1071             if (d_invalidate(alias) == 0) {
1072                 dput(alias);
1073             } else {
1074                 iput(ip);
1075                 crfree(credp);
1076                 return alias;
1077             }
1078         }
1079     }
1080     newdp = d_splice_alias(ip, dp);
1081
1082     crfree(credp);
1083
1084     /* It's ok for the file to not be found. That's noted by the caller by
1085      * seeing that the dp->d_inode field is NULL.
1086      */
1087     if (!code || code == ENOENT)
1088         return newdp;
1089     else 
1090         return ERR_PTR(afs_convert_code(code));
1091 }
1092
1093 static int
1094 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1095 {
1096     int code;
1097     cred_t *credp = crref();
1098     const char *name = newdp->d_name.name;
1099     struct inode *oldip = olddp->d_inode;
1100
1101     /* If afs_link returned the vnode, we could instantiate the
1102      * dentry. Since it's not, we drop this one and do a new lookup.
1103      */
1104     d_drop(newdp);
1105
1106     AFS_GLOCK();
1107     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1108
1109     AFS_GUNLOCK();
1110     crfree(credp);
1111     return afs_convert_code(code);
1112 }
1113
1114 /* We have to have a Linux specific sillyrename function, because we
1115  * also have to keep the dcache up to date when we're doing a silly
1116  * rename - so we don't want the generic vnodeops doing this behind our
1117  * back.
1118  */
1119
1120 static int
1121 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1122                       cred_t *credp)
1123 {
1124     struct vcache *tvc = VTOAFS(dentry->d_inode);
1125     struct dentry *__dp = NULL;
1126     char *__name = NULL;
1127     int code;
1128
1129     if (afs_linux_nfsfs_renamed(dentry))
1130         return EBUSY;
1131
1132     do {
1133         dput(__dp);
1134
1135         AFS_GLOCK();
1136         if (__name)
1137             osi_FreeSmallSpace(__name);
1138         __name = afs_newname();
1139         AFS_GUNLOCK();
1140
1141         __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1142
1143         if (IS_ERR(__dp)) {
1144             osi_FreeSmallSpace(__name);
1145             return EBUSY;
1146         }
1147     } while (__dp->d_inode != NULL);
1148
1149     AFS_GLOCK();
1150     code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1151                       VTOAFS(dir), (char *)__dp->d_name.name,
1152                       credp);
1153     if (!code) {
1154         tvc->mvid = (void *) __name;
1155         crhold(credp);
1156         if (tvc->uncred) {
1157             crfree(tvc->uncred);
1158         }
1159         tvc->uncred = credp;
1160         tvc->f.states |= CUnlinked;
1161         afs_linux_set_nfsfs_renamed(dentry);
1162     } else {
1163         osi_FreeSmallSpace(__name);
1164     }
1165     AFS_GUNLOCK();
1166
1167     if (!code) {
1168         __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1169         d_move(dentry, __dp);
1170     }
1171     dput(__dp);
1172
1173     return code;
1174 }
1175
1176
1177 static int
1178 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1179 {
1180     int code = EBUSY;
1181     cred_t *credp = crref();
1182     const char *name = dp->d_name.name;
1183     struct vcache *tvc = VTOAFS(dp->d_inode);
1184
1185     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1186                                 && !(tvc->f.states & CUnlinked)) {
1187
1188         code = afs_linux_sillyrename(dip, dp, credp);
1189     } else {
1190         AFS_GLOCK();
1191         code = afs_remove(VTOAFS(dip), (char *)name, credp);
1192         AFS_GUNLOCK();
1193         if (!code)
1194             d_drop(dp);
1195     }
1196
1197     crfree(credp);
1198     return afs_convert_code(code);
1199 }
1200
1201
1202 static int
1203 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1204 {
1205     int code;
1206     cred_t *credp = crref();
1207     struct vattr vattr;
1208     const char *name = dp->d_name.name;
1209
1210     /* If afs_symlink returned the vnode, we could instantiate the
1211      * dentry. Since it's not, we drop this one and do a new lookup.
1212      */
1213     d_drop(dp);
1214
1215     VATTR_NULL(&vattr);
1216     AFS_GLOCK();
1217     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1218     AFS_GUNLOCK();
1219     crfree(credp);
1220     return afs_convert_code(code);
1221 }
1222
1223 static int
1224 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1225 {
1226     int code;
1227     cred_t *credp = crref();
1228     struct vcache *tvcp = NULL;
1229     struct vattr vattr;
1230     const char *name = dp->d_name.name;
1231
1232     VATTR_NULL(&vattr);
1233     vattr.va_mask = ATTR_MODE;
1234     vattr.va_mode = mode;
1235     AFS_GLOCK();
1236     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1237
1238     if (tvcp) {
1239         struct inode *ip = AFSTOV(tvcp);
1240
1241         afs_getattr(tvcp, &vattr, credp);
1242         afs_fill_inode(ip, &vattr);
1243
1244         dp->d_op = &afs_dentry_operations;
1245         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1246         d_instantiate(dp, ip);
1247     }
1248     AFS_GUNLOCK();
1249
1250     crfree(credp);
1251     return afs_convert_code(code);
1252 }
1253
1254 static int
1255 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1256 {
1257     int code;
1258     cred_t *credp = crref();
1259     const char *name = dp->d_name.name;
1260
1261     /* locking kernel conflicts with glock? */
1262
1263     AFS_GLOCK();
1264     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1265     AFS_GUNLOCK();
1266
1267     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1268      * that failed because a directory is not empty. So, we map
1269      * EEXIST to ENOTEMPTY on linux.
1270      */
1271     if (code == EEXIST) {
1272         code = ENOTEMPTY;
1273     }
1274
1275     if (!code) {
1276         d_drop(dp);
1277     }
1278
1279     crfree(credp);
1280     return afs_convert_code(code);
1281 }
1282
1283
1284 static int
1285 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1286                  struct inode *newip, struct dentry *newdp)
1287 {
1288     int code;
1289     cred_t *credp = crref();
1290     const char *oldname = olddp->d_name.name;
1291     const char *newname = newdp->d_name.name;
1292     struct dentry *rehash = NULL;
1293
1294     /* Prevent any new references during rename operation. */
1295
1296     if (!d_unhashed(newdp)) {
1297         d_drop(newdp);
1298         rehash = newdp;
1299     }
1300
1301     if (atomic_read(&olddp->d_count) > 1)
1302         shrink_dcache_parent(olddp);
1303
1304     AFS_GLOCK();
1305     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1306     AFS_GUNLOCK();
1307
1308     if (!code)
1309         olddp->d_time = 0;      /* force to revalidate */
1310
1311     if (rehash)
1312         d_rehash(rehash);
1313
1314     crfree(credp);
1315     return afs_convert_code(code);
1316 }
1317
1318
1319 /* afs_linux_ireadlink 
1320  * Internal readlink which can return link contents to user or kernel space.
1321  * Note that the buffer is NOT supposed to be null-terminated.
1322  */
1323 static int
1324 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1325 {
1326     int code;
1327     cred_t *credp = crref();
1328     uio_t tuio;
1329     struct iovec iov;
1330
1331     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1332     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1333     crfree(credp);
1334
1335     if (!code)
1336         return maxlen - tuio.uio_resid;
1337     else
1338         return afs_convert_code(code);
1339 }
1340
1341 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1342 /* afs_linux_readlink 
1343  * Fill target (which is in user space) with contents of symlink.
1344  */
1345 static int
1346 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1347 {
1348     int code;
1349     struct inode *ip = dp->d_inode;
1350
1351     AFS_GLOCK();
1352     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1353     AFS_GUNLOCK();
1354     return code;
1355 }
1356
1357
1358 /* afs_linux_follow_link
1359  * a file system dependent link following routine.
1360  */
1361 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1362 {
1363     int code;
1364     char *name;
1365
1366     name = osi_Alloc(PATH_MAX);
1367     if (!name) {
1368         return -EIO;
1369     }
1370
1371     AFS_GLOCK();
1372     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1373     AFS_GUNLOCK();
1374
1375     if (code < 0) {
1376         goto out;
1377     }
1378
1379     name[code] = '\0';
1380     code = vfs_follow_link(nd, name);
1381
1382 out:
1383     osi_Free(name, PATH_MAX);
1384
1385     return code;
1386 }
1387
1388 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1389
1390 #if defined(AFS_CACHE_BYPASS)
1391 #endif /* defined(AFS_CACHE_BYPASS */
1392
1393 /* Populate a page by filling it from the cache file pointed at by cachefp
1394  * (which contains indicated chunk)
1395  * If task is NULL, the page copy occurs syncronously, and the routine
1396  * returns with page still locked. If task is non-NULL, then page copies
1397  * may occur in the background, and the page will be unlocked when it is
1398  * ready for use.
1399  */
1400 static int
1401 afs_linux_read_cache(struct file *cachefp, struct page *page,
1402                      int chunk, struct pagevec *lrupv,
1403                      struct afs_pagecopy_task *task) {
1404     loff_t offset = page_offset(page);
1405     struct page *newpage, *cachepage;
1406     struct address_space *cachemapping;
1407     int pageindex;
1408     int code = 0;
1409
1410     cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1411     newpage = NULL;
1412     cachepage = NULL;
1413
1414     /* From our offset, we now need to work out which page in the disk
1415      * file it corresponds to. This will be fun ... */
1416     pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1417
1418     while (cachepage == NULL) {
1419         cachepage = find_get_page(cachemapping, pageindex);
1420         if (!cachepage) {
1421             if (!newpage)
1422                 newpage = page_cache_alloc_cold(cachemapping);
1423             if (!newpage) {
1424                 code = -ENOMEM;
1425                 goto out;
1426             }
1427
1428             code = add_to_page_cache(newpage, cachemapping,
1429                                      pageindex, GFP_KERNEL);
1430             if (code == 0) {
1431                 cachepage = newpage;
1432                 newpage = NULL;
1433
1434                 page_cache_get(cachepage);
1435                 if (!pagevec_add(lrupv, cachepage))
1436                     __pagevec_lru_add_file(lrupv);
1437
1438             } else {
1439                 page_cache_release(newpage);
1440                 newpage = NULL;
1441                 if (code != -EEXIST)
1442                     goto out;
1443             }
1444         } else {
1445             lock_page(cachepage);
1446         }
1447     }
1448
1449     if (!PageUptodate(cachepage)) {
1450         ClearPageError(cachepage);
1451         code = cachemapping->a_ops->readpage(NULL, cachepage);
1452         if (!code && !task) {
1453             wait_on_page_locked(cachepage);
1454         }
1455     } else {
1456         unlock_page(cachepage);
1457     }
1458
1459     if (!code) {
1460         if (PageUptodate(cachepage)) {
1461             copy_highpage(page, cachepage);
1462             flush_dcache_page(page);
1463             SetPageUptodate(page);
1464
1465             if (task)
1466                 unlock_page(page);
1467         } else if (task) {
1468             afs_pagecopy_queue_page(task, cachepage, page);
1469         } else {
1470             code = -EIO;
1471         }
1472     }
1473
1474     if (code && task) {
1475         unlock_page(page);
1476     }
1477
1478 out:
1479     if (cachepage)
1480         page_cache_release(cachepage);
1481
1482     return code;
1483 }
1484
1485 static int inline
1486 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1487 {
1488     loff_t offset = page_offset(pp);
1489     struct inode *ip = FILE_INODE(fp);
1490     struct vcache *avc = VTOAFS(ip);
1491     struct dcache *tdc;
1492     struct file *cacheFp = NULL;
1493     int code;
1494     int dcLocked = 0;
1495     struct pagevec lrupv;
1496
1497     /* Not a UFS cache, don't do anything */
1498     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1499         return 0;
1500
1501     /* Can't do anything if the vcache isn't statd , or if the read
1502      * crosses a chunk boundary.
1503      */
1504     if (!(avc->f.states & CStatd) ||
1505         AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1506         return 0;
1507     }
1508
1509     ObtainWriteLock(&avc->lock, 911);
1510
1511     /* XXX - See if hinting actually makes things faster !!! */
1512
1513     /* See if we have a suitable entry already cached */
1514     tdc = avc->dchint;
1515
1516     if (tdc) {
1517         /* We need to lock xdcache, then dcache, to handle situations where
1518          * the hint is on the free list. However, we can't safely do this
1519          * according to the locking hierarchy. So, use a non blocking lock.
1520          */
1521         ObtainReadLock(&afs_xdcache);
1522         dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1523
1524         if (dcLocked && (tdc->index != NULLIDX)
1525             && !FidCmp(&tdc->f.fid, &avc->f.fid)
1526             && tdc->f.chunk == AFS_CHUNK(offset)
1527             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1528             /* Bonus - the hint was correct */
1529             afs_RefDCache(tdc);
1530         } else {
1531             /* Only destroy the hint if its actually invalid, not if there's
1532              * just been a locking failure */
1533             if (dcLocked) {
1534                 ReleaseReadLock(&tdc->lock);
1535                 avc->dchint = NULL;
1536             }
1537
1538             tdc = NULL;
1539             dcLocked = 0;
1540         }
1541         ReleaseReadLock(&afs_xdcache);
1542     }
1543
1544     /* No hint, or hint is no longer valid - see if we can get something
1545      * directly from the dcache
1546      */
1547     if (!tdc)
1548         tdc = afs_FindDCache(avc, offset);
1549
1550     if (!tdc) {
1551         ReleaseWriteLock(&avc->lock);
1552         return 0;
1553     }
1554
1555     if (!dcLocked)
1556         ObtainReadLock(&tdc->lock);
1557
1558     /* Is the dcache we've been given currently up to date */
1559     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1560         (tdc->dflags & DFFetching)) {
1561         ReleaseWriteLock(&avc->lock);
1562         ReleaseReadLock(&tdc->lock);
1563         afs_PutDCache(tdc);
1564         return 0;
1565     }
1566
1567     /* Update our hint for future abuse */
1568     avc->dchint = tdc;
1569
1570     /* Okay, so we've now got a cache file that is up to date */
1571
1572     /* XXX - I suspect we should be locking the inodes before we use them! */
1573     AFS_GUNLOCK();
1574     cacheFp = afs_linux_raw_open(&tdc->f.inode);
1575     pagevec_init(&lrupv, 0);
1576
1577     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1578
1579     if (pagevec_count(&lrupv))
1580        __pagevec_lru_add_file(&lrupv);
1581
1582     filp_close(cacheFp, NULL);
1583     AFS_GLOCK();
1584
1585     ReleaseReadLock(&tdc->lock);
1586     ReleaseWriteLock(&avc->lock);
1587     afs_PutDCache(tdc);
1588
1589     *codep = code;
1590     return 1;
1591 }
1592
1593 /* afs_linux_readpage
1594  *
1595  * This function is split into two, because prepare_write/begin_write
1596  * require a readpage call which doesn't unlock the resulting page upon
1597  * success.
1598  */
1599 static int
1600 afs_linux_fillpage(struct file *fp, struct page *pp)
1601 {
1602     afs_int32 code;
1603     char *address;
1604     uio_t *auio;
1605     struct iovec *iovecp;
1606     struct inode *ip = FILE_INODE(fp);
1607     afs_int32 cnt = page_count(pp);
1608     struct vcache *avc = VTOAFS(ip);
1609     afs_offs_t offset = page_offset(pp);
1610     cred_t *credp;
1611
1612     AFS_GLOCK();
1613     if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1614         AFS_GUNLOCK();
1615         return code;
1616     }
1617     AFS_GUNLOCK();
1618
1619     credp = crref();
1620     address = kmap(pp);
1621     ClearPageError(pp);
1622
1623     auio = osi_Alloc(sizeof(uio_t));
1624     iovecp = osi_Alloc(sizeof(struct iovec));
1625
1626     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1627               AFS_UIOSYS);
1628
1629     AFS_GLOCK();
1630     AFS_DISCON_LOCK();
1631     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1632                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1633                99999);  /* not a possible code value */
1634
1635     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1636         
1637     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1638                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1639                code);
1640     AFS_DISCON_UNLOCK();
1641     AFS_GUNLOCK();
1642     if (!code) {
1643         /* XXX valid for no-cache also?  Check last bits of files... :)
1644          * Cognate code goes in afs_NoCacheFetchProc.  */
1645         if (auio->uio_resid)    /* zero remainder of page */
1646              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1647                     auio->uio_resid);
1648
1649         flush_dcache_page(pp);
1650         SetPageUptodate(pp);
1651     } /* !code */
1652
1653     kunmap(pp);
1654
1655     osi_Free(auio, sizeof(uio_t));
1656     osi_Free(iovecp, sizeof(struct iovec));
1657
1658     crfree(credp);
1659     return afs_convert_code(code);
1660 }
1661
1662 static int
1663 afs_linux_prefetch(struct file *fp, struct page *pp)
1664 {
1665     int code = 0;
1666     struct vcache *avc = VTOAFS(FILE_INODE(fp));
1667     afs_offs_t offset = page_offset(pp);
1668
1669     if (AFS_CHUNKOFFSET(offset) == 0) {
1670         struct dcache *tdc;
1671         struct vrequest treq;
1672         cred_t *credp;
1673
1674         credp = crref();
1675         AFS_GLOCK();
1676         code = afs_InitReq(&treq, credp);
1677         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1678             tdc = afs_FindDCache(avc, offset);
1679             if (tdc) {
1680                 if (!(tdc->mflags & DFNextStarted))
1681                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1682                     afs_PutDCache(tdc);
1683             }
1684             ReleaseWriteLock(&avc->lock);
1685         }
1686         AFS_GUNLOCK();
1687         crfree(credp);
1688     }
1689     return afs_convert_code(code);
1690
1691 }
1692
1693 #if defined(AFS_CACHE_BYPASS)
1694
1695 static int
1696 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1697                            struct list_head *page_list, unsigned num_pages)
1698 {
1699     afs_int32 page_ix;
1700     uio_t *auio;
1701     afs_offs_t offset;
1702     struct iovec* iovecp;
1703     struct nocache_read_request *ancr;
1704     struct page *pp;
1705     struct pagevec lrupv;
1706     afs_int32 code = 0;
1707
1708     cred_t *credp;
1709     struct inode *ip = FILE_INODE(fp);
1710     struct vcache *avc = VTOAFS(ip);
1711     afs_int32 base_index = 0;
1712     afs_int32 page_count = 0;
1713     afs_int32 isize;
1714
1715     /* background thread must free: iovecp, auio, ancr */
1716     iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1717
1718     auio = osi_Alloc(sizeof(uio_t));
1719     auio->uio_iov = iovecp;
1720     auio->uio_iovcnt = num_pages;
1721     auio->uio_flag = UIO_READ;
1722     auio->uio_seg = AFS_UIOSYS;
1723     auio->uio_resid = num_pages * PAGE_SIZE;
1724
1725     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1726     ancr->auio = auio;
1727     ancr->offset = auio->uio_offset;
1728     ancr->length = auio->uio_resid;
1729
1730     pagevec_init(&lrupv, 0);
1731
1732     for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1733
1734         if(list_empty(page_list))
1735             break;
1736
1737         pp = list_entry(page_list->prev, struct page, lru);
1738         /* If we allocate a page and don't remove it from page_list,
1739          * the page cache gets upset. */
1740         list_del(&pp->lru);
1741         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1742         if(pp->index > isize) {
1743             if(PageLocked(pp))
1744                 unlock_page(pp);
1745             continue;
1746         }
1747
1748         if(page_ix == 0) {
1749             offset = page_offset(pp);
1750             auio->uio_offset = offset;
1751             base_index = pp->index;
1752         }
1753         iovecp[page_ix].iov_len = PAGE_SIZE;
1754         code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1755         if(base_index != pp->index) {
1756             if(PageLocked(pp))
1757                  unlock_page(pp);
1758             page_cache_release(pp);
1759             iovecp[page_ix].iov_base = (void *) 0;
1760             base_index++;
1761             ancr->length -= PAGE_SIZE;
1762             continue;
1763         }
1764         base_index++;
1765         if(code) {
1766             if(PageLocked(pp))
1767                 unlock_page(pp);
1768             page_cache_release(pp);
1769             iovecp[page_ix].iov_base = (void *) 0;
1770         } else {
1771             page_count++;
1772             if(!PageLocked(pp)) {
1773                 lock_page(pp);
1774             }
1775
1776             /* increment page refcount--our original design assumed
1777              * that locking it would effectively pin it;  protect
1778              * ourselves from the possiblity that this assumption is
1779              * is faulty, at low cost (provided we do not fail to
1780              * do the corresponding decref on the other side) */
1781             get_page(pp);
1782
1783             /* save the page for background map */
1784             iovecp[page_ix].iov_base = (void*) pp;
1785
1786             /* and put it on the LRU cache */
1787             if (!pagevec_add(&lrupv, pp))
1788                 __pagevec_lru_add_file(&lrupv);
1789         }
1790     }
1791
1792     /* If there were useful pages in the page list, make sure all pages
1793      * are in the LRU cache, then schedule the read */
1794     if(page_count) {
1795         if (pagevec_count(&lrupv))
1796             __pagevec_lru_add_file(&lrupv);
1797         credp = crref();
1798         code = afs_ReadNoCache(avc, ancr, credp);
1799         crfree(credp);
1800     } else {
1801         /* If there is nothing for the background thread to handle,
1802          * it won't be freeing the things that we never gave it */
1803         osi_Free(iovecp, num_pages * sizeof(struct iovec));
1804         osi_Free(auio, sizeof(uio_t));
1805         osi_Free(ancr, sizeof(struct nocache_read_request));
1806     }
1807     /* we do not flush, release, or unmap pages--that will be
1808      * done for us by the background thread as each page comes in
1809      * from the fileserver */
1810     return afs_convert_code(code);
1811 }
1812
1813
1814 static int
1815 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1816 {
1817     cred_t *credp = NULL;
1818     uio_t *auio;
1819     struct iovec *iovecp;
1820     struct nocache_read_request *ancr;
1821     int code;
1822
1823     /*
1824      * Special case: if page is at or past end of file, just zero it and set
1825      * it as up to date.
1826      */
1827     if (page_offset(pp) >=  i_size_read(fp->f_mapping->host)) {
1828         zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1829         SetPageUptodate(pp);
1830         unlock_page(pp);
1831         return 0;
1832     }
1833
1834     ClearPageError(pp);
1835
1836     /* receiver frees */
1837     auio = osi_Alloc(sizeof(uio_t));
1838     iovecp = osi_Alloc(sizeof(struct iovec));
1839
1840     /* address can be NULL, because we overwrite it with 'pp', below */
1841     setup_uio(auio, iovecp, NULL, page_offset(pp),
1842               PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1843
1844     /* save the page for background map */
1845     get_page(pp); /* see above */
1846     auio->uio_iov->iov_base = (void*) pp;
1847     /* the background thread will free this */
1848     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1849     ancr->auio = auio;
1850     ancr->offset = page_offset(pp);
1851     ancr->length = PAGE_SIZE;
1852
1853     credp = crref();
1854     code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1855     crfree(credp);
1856
1857     return afs_convert_code(code);
1858 }
1859
1860 static inline int
1861 afs_linux_can_bypass(struct inode *ip) {
1862     switch(cache_bypass_strategy) {
1863         case NEVER_BYPASS_CACHE:
1864             return 0;
1865         case ALWAYS_BYPASS_CACHE:
1866             return 1;
1867         case LARGE_FILES_BYPASS_CACHE:
1868             if(i_size_read(ip) > cache_bypass_threshold)
1869                 return 1;
1870         default:
1871             return 0;
1872      }
1873 }
1874
1875 /* Check if a file is permitted to bypass the cache by policy, and modify
1876  * the cache bypass state recorded for that file */
1877
1878 static inline int
1879 afs_linux_bypass_check(struct inode *ip) {
1880     cred_t* credp;
1881
1882     int bypass = afs_linux_can_bypass(ip);
1883
1884     credp = crref();
1885     trydo_cache_transition(VTOAFS(ip), credp, bypass);
1886     crfree(credp);
1887
1888     return bypass;
1889 }
1890
1891 #else
1892 static inline int
1893 afs_linux_bypass_check(struct inode *ip) {
1894     return 0;
1895 }
1896 static inline int
1897 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1898     return 0;
1899 }
1900 static inline int
1901 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1902                     struct list_head *page_list, unsigned int num_pages) {
1903     return 0;
1904 }
1905 #endif
1906
1907 static int
1908 afs_linux_readpage(struct file *fp, struct page *pp)
1909 {
1910     int code;
1911
1912     if (afs_linux_bypass_check(FILE_INODE(fp))) {
1913         code = afs_linux_bypass_readpage(fp, pp);
1914     } else {
1915         code = afs_linux_fillpage(fp, pp);
1916         if (!code)
1917             code = afs_linux_prefetch(fp, pp);
1918         unlock_page(pp);
1919     }
1920
1921     return code;
1922 }
1923
1924 /* Readpages reads a number of pages for a particular file. We use
1925  * this to optimise the reading, by limiting the number of times upon which
1926  * we have to lookup, lock and open vcaches and dcaches
1927  */
1928
1929 static int
1930 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1931                     struct list_head *page_list, unsigned int num_pages)
1932 {
1933     struct inode *inode = mapping->host;
1934     struct vcache *avc = VTOAFS(inode);
1935     struct dcache *tdc;
1936     struct file *cacheFp = NULL;
1937     int code;
1938     unsigned int page_idx;
1939     loff_t offset;
1940     struct pagevec lrupv;
1941     struct afs_pagecopy_task *task;
1942
1943     if (afs_linux_bypass_check(inode))
1944         return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1945
1946     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1947         return 0;
1948
1949     AFS_GLOCK();
1950     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1951         AFS_GUNLOCK();
1952         return code;
1953     }
1954
1955     ObtainWriteLock(&avc->lock, 912);
1956     AFS_GUNLOCK();
1957
1958     task = afs_pagecopy_init_task();
1959
1960     tdc = NULL;
1961     pagevec_init(&lrupv, 0);
1962     for (page_idx = 0; page_idx < num_pages; page_idx++) {
1963         struct page *page = list_entry(page_list->prev, struct page, lru);
1964         list_del(&page->lru);
1965         offset = page_offset(page);
1966
1967         if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1968             AFS_GLOCK();
1969             ReleaseReadLock(&tdc->lock);
1970             afs_PutDCache(tdc);
1971             AFS_GUNLOCK();
1972             tdc = NULL;
1973             if (cacheFp)
1974                 filp_close(cacheFp, NULL);
1975         }
1976
1977         if (!tdc) {
1978             AFS_GLOCK();
1979             if ((tdc = afs_FindDCache(avc, offset))) {
1980                 ObtainReadLock(&tdc->lock);
1981                 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1982                     (tdc->dflags & DFFetching)) {
1983                     ReleaseReadLock(&tdc->lock);
1984                     afs_PutDCache(tdc);
1985                     tdc = NULL;
1986                 }
1987             }
1988             AFS_GUNLOCK();
1989             if (tdc)
1990                 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1991         }
1992
1993         if (tdc && !add_to_page_cache(page, mapping, page->index,
1994                                       GFP_KERNEL)) {
1995             page_cache_get(page);
1996             if (!pagevec_add(&lrupv, page))
1997                 __pagevec_lru_add_file(&lrupv);
1998
1999             afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2000         }
2001         page_cache_release(page);
2002     }
2003     if (pagevec_count(&lrupv))
2004        __pagevec_lru_add_file(&lrupv);
2005
2006     if (tdc)
2007         filp_close(cacheFp, NULL);
2008
2009     afs_pagecopy_put_task(task);
2010
2011     AFS_GLOCK();
2012     if (tdc) {
2013         ReleaseReadLock(&tdc->lock);
2014         afs_PutDCache(tdc);
2015     }
2016
2017     ReleaseWriteLock(&avc->lock);
2018     AFS_GUNLOCK();
2019     return 0;
2020 }
2021
2022 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2023  * locked */
2024 static inline int
2025 afs_linux_prepare_writeback(struct vcache *avc) {
2026     if (avc->f.states & CPageWrite) {
2027         return AOP_WRITEPAGE_ACTIVATE;
2028     }
2029     avc->f.states |= CPageWrite;
2030     return 0;
2031 }
2032
2033 static inline int
2034 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2035     struct vrequest treq;
2036     int code = 0;
2037
2038     if (!afs_InitReq(&treq, credp))
2039         code = afs_DoPartialWrite(avc, &treq);
2040
2041     return afs_convert_code(code);
2042 }
2043
2044 static inline void
2045 afs_linux_complete_writeback(struct vcache *avc) {
2046     avc->f.states &= ~CPageWrite;
2047 }
2048
2049 /* Writeback a given page syncronously. Called with no AFS locks held */
2050 static int
2051 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2052                          unsigned long offset, unsigned int count,
2053                          cred_t *credp)
2054 {
2055     struct vcache *vcp = VTOAFS(ip);
2056     char *buffer;
2057     afs_offs_t base;
2058     int code = 0;
2059     uio_t tuio;
2060     struct iovec iovec;
2061     int f_flags = 0;
2062
2063     buffer = kmap(pp) + offset;
2064     base = page_offset(pp) + offset;
2065
2066     AFS_GLOCK();
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, 99999);
2070
2071     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2072
2073     code = afs_write(vcp, &tuio, f_flags, credp, 0);
2074
2075     i_size_write(ip, vcp->f.m.Length);
2076     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2077
2078     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2079
2080     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2081                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2082                ICL_TYPE_INT32, code);
2083
2084     AFS_GUNLOCK();
2085     kunmap(pp);
2086
2087     return code;
2088 }
2089
2090 static int
2091 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2092                          unsigned long offset, unsigned int count)
2093 {
2094     int code;
2095     int code1 = 0;
2096     struct vcache *vcp = VTOAFS(ip);
2097     cred_t *credp;
2098
2099     /* Catch recursive writeback. This occurs if the kernel decides
2100      * writeback is required whilst we are writing to the cache, or
2101      * flushing to the server. When we're running syncronously (as
2102      * opposed to from writepage) we can't actually do anything about
2103      * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2104      */
2105     AFS_GLOCK();
2106     ObtainWriteLock(&vcp->lock, 532);
2107     afs_linux_prepare_writeback(vcp);
2108     ReleaseWriteLock(&vcp->lock);
2109     AFS_GUNLOCK();
2110
2111     credp = crref();
2112     code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2113
2114     AFS_GLOCK();
2115     ObtainWriteLock(&vcp->lock, 533);
2116     if (code > 0)
2117         code1 = afs_linux_dopartialwrite(vcp, credp);
2118     afs_linux_complete_writeback(vcp);
2119     ReleaseWriteLock(&vcp->lock);
2120     AFS_GUNLOCK();
2121     crfree(credp);
2122
2123     if (code1)
2124         return code1;
2125
2126     return code;
2127 }
2128
2129 static int
2130 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2131 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2132 #else
2133 afs_linux_writepage(struct page *pp)
2134 #endif
2135 {
2136     struct address_space *mapping = pp->mapping;
2137     struct inode *inode;
2138     struct vcache *vcp;
2139     cred_t *credp;
2140     unsigned int to = PAGE_CACHE_SIZE;
2141     loff_t isize;
2142     int code = 0;
2143     int code1 = 0;
2144
2145     if (PageReclaim(pp)) {
2146         return AOP_WRITEPAGE_ACTIVATE;
2147         /* XXX - Do we need to redirty the page here? */
2148     }
2149
2150     page_cache_get(pp);
2151
2152     inode = mapping->host;
2153     vcp = VTOAFS(inode);
2154     isize = i_size_read(inode);
2155
2156     /* Don't defeat an earlier truncate */
2157     if (page_offset(pp) > isize) {
2158         set_page_writeback(pp);
2159         unlock_page(pp);
2160         goto done;
2161     }
2162
2163     AFS_GLOCK();
2164     ObtainWriteLock(&vcp->lock, 537);
2165     code = afs_linux_prepare_writeback(vcp);
2166     if (code == AOP_WRITEPAGE_ACTIVATE) {
2167         /* WRITEPAGE_ACTIVATE is the only return value that permits us
2168          * to return with the page still locked */
2169         ReleaseWriteLock(&vcp->lock);
2170         AFS_GUNLOCK();
2171         return code;
2172     }
2173
2174     /* Grab the creds structure currently held in the vnode, and
2175      * get a reference to it, in case it goes away ... */
2176     credp = vcp->cred;
2177     if (credp)
2178         crhold(credp);
2179     else
2180         credp = crref();
2181     ReleaseWriteLock(&vcp->lock);
2182     AFS_GUNLOCK();
2183
2184     set_page_writeback(pp);
2185
2186     SetPageUptodate(pp);
2187
2188     /* We can unlock the page here, because it's protected by the
2189      * page_writeback flag. This should make us less vulnerable to
2190      * deadlocking in afs_write and afs_DoPartialWrite
2191      */
2192     unlock_page(pp);
2193
2194     /* If this is the final page, then just write the number of bytes that
2195      * are actually in it */
2196     if ((isize - page_offset(pp)) < to )
2197         to = isize - page_offset(pp);
2198
2199     code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2200
2201     AFS_GLOCK();
2202     ObtainWriteLock(&vcp->lock, 538);
2203
2204     /* As much as we might like to ignore a file server error here,
2205      * and just try again when we close(), unfortunately StoreAllSegments
2206      * will invalidate our chunks if the server returns a permanent error,
2207      * so we need to at least try and get that error back to the user
2208      */
2209     if (code == to)
2210         code1 = afs_linux_dopartialwrite(vcp, credp);
2211
2212     afs_linux_complete_writeback(vcp);
2213     ReleaseWriteLock(&vcp->lock);
2214     crfree(credp);
2215     AFS_GUNLOCK();
2216
2217 done:
2218     end_page_writeback(pp);
2219     page_cache_release(pp);
2220
2221     if (code1)
2222         return code1;
2223
2224     if (code == to)
2225         return 0;
2226
2227     return code;
2228 }
2229
2230 /* afs_linux_permission
2231  * Check access rights - returns error if can't check or permission denied.
2232  */
2233 static int
2234 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2235 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2236 #else
2237 afs_linux_permission(struct inode *ip, int mode)
2238 #endif
2239 {
2240     int code;
2241     cred_t *credp = crref();
2242     int tmp = 0;
2243
2244     AFS_GLOCK();
2245     if (mode & MAY_EXEC)
2246         tmp |= VEXEC;
2247     if (mode & MAY_READ)
2248         tmp |= VREAD;
2249     if (mode & MAY_WRITE)
2250         tmp |= VWRITE;
2251     code = afs_access(VTOAFS(ip), tmp, credp);
2252
2253     AFS_GUNLOCK();
2254     crfree(credp);
2255     return afs_convert_code(code);
2256 }
2257
2258 static int
2259 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2260                        unsigned to)
2261 {
2262     int code;
2263     struct inode *inode = FILE_INODE(file);
2264     loff_t pagebase = page_offset(page);
2265
2266     if (i_size_read(inode) < (pagebase + offset))
2267         i_size_write(inode, pagebase + offset);
2268
2269     if (PageChecked(page)) {
2270         SetPageUptodate(page);
2271         ClearPageChecked(page);
2272     }
2273
2274     code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2275
2276     return code;
2277 }
2278
2279 static int
2280 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2281                         unsigned to)
2282 {
2283
2284     /* http://kerneltrap.org/node/4941 details the expected behaviour of
2285      * prepare_write. Essentially, if the page exists within the file,
2286      * and is not being fully written, then we should populate it.
2287      */
2288
2289     if (!PageUptodate(page)) {
2290         loff_t pagebase = page_offset(page);
2291         loff_t isize = i_size_read(page->mapping->host);
2292
2293         /* Is the location we are writing to beyond the end of the file? */
2294         if (pagebase >= isize ||
2295             ((from == 0) && (pagebase + to) >= isize)) {
2296             zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2297             SetPageChecked(page);
2298         /* Are we we writing a full page */
2299         } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2300             SetPageChecked(page);
2301         /* Is the page readable, if it's wronly, we don't care, because we're
2302          * not actually going to read from it ... */
2303         } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2304             /* We don't care if fillpage fails, because if it does the page
2305              * won't be marked as up to date
2306              */
2307             afs_linux_fillpage(file, page);
2308         }
2309     }
2310     return 0;
2311 }
2312
2313 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2314 static int
2315 afs_linux_write_end(struct file *file, struct address_space *mapping,
2316                                 loff_t pos, unsigned len, unsigned copied,
2317                                 struct page *page, void *fsdata)
2318 {
2319     int code;
2320     unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2321
2322     code = afs_linux_commit_write(file, page, from, from + len);
2323
2324     unlock_page(page);
2325     page_cache_release(page);
2326     return code;
2327 }
2328
2329 static int
2330 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2331                                 loff_t pos, unsigned len, unsigned flags,
2332                                 struct page **pagep, void **fsdata)
2333 {
2334     struct page *page;
2335     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2336     unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2337     int code;
2338
2339     page = grab_cache_page_write_begin(mapping, index, flags);
2340     *pagep = page;
2341
2342     code = afs_linux_prepare_write(file, page, from, from + len);
2343     if (code) {
2344         unlock_page(page);
2345         page_cache_release(page);
2346     }
2347
2348     return code;
2349 }
2350 #endif
2351
2352
2353 static struct inode_operations afs_file_iops = {
2354   .permission =         afs_linux_permission,
2355   .getattr =            afs_linux_getattr,
2356   .setattr =            afs_notify_change,
2357 };
2358
2359 static struct address_space_operations afs_file_aops = {
2360   .readpage =           afs_linux_readpage,
2361   .readpages =          afs_linux_readpages,
2362   .writepage =          afs_linux_writepage,
2363 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2364   .write_begin =        afs_linux_write_begin,
2365   .write_end =          afs_linux_write_end,
2366 #else
2367   .commit_write =       afs_linux_commit_write,
2368   .prepare_write =      afs_linux_prepare_write,
2369 #endif
2370 };
2371
2372
2373 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2374  * by what sort of operation is allowed.....
2375  */
2376
2377 static struct inode_operations afs_dir_iops = {
2378   .setattr =            afs_notify_change,
2379   .create =             afs_linux_create,
2380   .lookup =             afs_linux_lookup,
2381   .link =               afs_linux_link,
2382   .unlink =             afs_linux_unlink,
2383   .symlink =            afs_linux_symlink,
2384   .mkdir =              afs_linux_mkdir,
2385   .rmdir =              afs_linux_rmdir,
2386   .rename =             afs_linux_rename,
2387   .getattr =            afs_linux_getattr,
2388   .permission =         afs_linux_permission,
2389 };
2390
2391 /* We really need a separate symlink set of ops, since do_follow_link()
2392  * determines if it _is_ a link by checking if the follow_link op is set.
2393  */
2394 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2395 static int
2396 afs_symlink_filler(struct file *file, struct page *page)
2397 {
2398     struct inode *ip = (struct inode *)page->mapping->host;
2399     char *p = (char *)kmap(page);
2400     int code;
2401
2402     AFS_GLOCK();
2403     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2404     AFS_GUNLOCK();
2405
2406     if (code < 0)
2407         goto fail;
2408     p[code] = '\0';             /* null terminate? */
2409
2410     SetPageUptodate(page);
2411     kunmap(page);
2412     unlock_page(page);
2413     return 0;
2414
2415   fail:
2416     SetPageError(page);
2417     kunmap(page);
2418     unlock_page(page);
2419     return code;
2420 }
2421
2422 static struct address_space_operations afs_symlink_aops = {
2423   .readpage =   afs_symlink_filler
2424 };
2425 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2426
2427 static struct inode_operations afs_symlink_iops = {
2428 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2429   .readlink =           page_readlink,
2430 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2431   .follow_link =        page_follow_link,
2432 # else
2433   .follow_link =        page_follow_link_light,
2434   .put_link =           page_put_link,
2435 # endif
2436 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2437   .readlink =           afs_linux_readlink,
2438   .follow_link =        afs_linux_follow_link,
2439 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2440   .setattr =            afs_notify_change,
2441 };
2442
2443 void
2444 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2445 {
2446         
2447     if (vattr)
2448         vattr2inode(ip, vattr);
2449
2450     ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2451 /* Reset ops if symlink or directory. */
2452     if (S_ISREG(ip->i_mode)) {
2453         ip->i_op = &afs_file_iops;
2454         ip->i_fop = &afs_file_fops;
2455         ip->i_data.a_ops = &afs_file_aops;
2456
2457     } else if (S_ISDIR(ip->i_mode)) {
2458         ip->i_op = &afs_dir_iops;
2459         ip->i_fop = &afs_dir_fops;
2460
2461     } else if (S_ISLNK(ip->i_mode)) {
2462         ip->i_op = &afs_symlink_iops;
2463 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2464         ip->i_data.a_ops = &afs_symlink_aops;
2465         ip->i_mapping = &ip->i_data;
2466 #endif
2467     }
2468
2469 }