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