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