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