de1a7039d1e3b418b377ff2bb50b20c1a9d495ea
[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         int retry = 1;
1413         struct dentry *alias;
1414
1415         while (retry) {
1416             retry = 0;
1417
1418             /* Try to invalidate an existing alias in favor of our new one */
1419             alias = d_find_alias(ip);
1420             /* But not if it's disconnected; then we want d_splice_alias below */
1421             if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1422                 if (d_invalidate(alias) == 0) {
1423                     /* there may be more aliases; try again until we run out */
1424                     retry = 1;
1425                 }
1426             }
1427
1428             dput(alias);
1429         }
1430
1431 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1432         ip->i_flags |= S_AUTOMOUNT;
1433 #endif
1434     }
1435     newdp = d_splice_alias(ip, dp);
1436
1437  done:
1438     crfree(credp);
1439
1440     /* It's ok for the file to not be found. That's noted by the caller by
1441      * seeing that the dp->d_inode field is NULL.
1442      */
1443     if (!code || code == ENOENT)
1444         return newdp;
1445     else 
1446         return ERR_PTR(afs_convert_code(code));
1447 }
1448
1449 static int
1450 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1451 {
1452     int code;
1453     cred_t *credp = crref();
1454     const char *name = newdp->d_name.name;
1455     struct inode *oldip = olddp->d_inode;
1456
1457     /* If afs_link returned the vnode, we could instantiate the
1458      * dentry. Since it's not, we drop this one and do a new lookup.
1459      */
1460     d_drop(newdp);
1461
1462     AFS_GLOCK();
1463     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1464
1465     AFS_GUNLOCK();
1466     crfree(credp);
1467     return afs_convert_code(code);
1468 }
1469
1470 /* We have to have a Linux specific sillyrename function, because we
1471  * also have to keep the dcache up to date when we're doing a silly
1472  * rename - so we don't want the generic vnodeops doing this behind our
1473  * back.
1474  */
1475
1476 static int
1477 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1478                       cred_t *credp)
1479 {
1480     struct vcache *tvc = VTOAFS(dentry->d_inode);
1481     struct dentry *__dp = NULL;
1482     char *__name = NULL;
1483     int code;
1484
1485     if (afs_linux_nfsfs_renamed(dentry))
1486         return EBUSY;
1487
1488     do {
1489         dput(__dp);
1490
1491         AFS_GLOCK();
1492         if (__name)
1493             osi_FreeSmallSpace(__name);
1494         __name = afs_newname();
1495         AFS_GUNLOCK();
1496
1497         __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1498
1499         if (IS_ERR(__dp)) {
1500             osi_FreeSmallSpace(__name);
1501             return EBUSY;
1502         }
1503     } while (__dp->d_inode != NULL);
1504
1505     AFS_GLOCK();
1506     code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1507                       VTOAFS(dir), (char *)__dp->d_name.name,
1508                       credp);
1509     if (!code) {
1510         tvc->mvid = (void *) __name;
1511         crhold(credp);
1512         if (tvc->uncred) {
1513             crfree(tvc->uncred);
1514         }
1515         tvc->uncred = credp;
1516         tvc->f.states |= CUnlinked;
1517         afs_linux_set_nfsfs_renamed(dentry);
1518     } else {
1519         osi_FreeSmallSpace(__name);
1520     }
1521     AFS_GUNLOCK();
1522
1523     if (!code) {
1524         __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1525         d_move(dentry, __dp);
1526     }
1527     dput(__dp);
1528
1529     return code;
1530 }
1531
1532
1533 static int
1534 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1535 {
1536     int code = EBUSY;
1537     cred_t *credp = crref();
1538     const char *name = dp->d_name.name;
1539     struct vcache *tvc = VTOAFS(dp->d_inode);
1540
1541     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1542                                 && !(tvc->f.states & CUnlinked)) {
1543
1544         code = afs_linux_sillyrename(dip, dp, credp);
1545     } else {
1546         AFS_GLOCK();
1547         code = afs_remove(VTOAFS(dip), (char *)name, credp);
1548         AFS_GUNLOCK();
1549         if (!code)
1550             d_drop(dp);
1551     }
1552
1553     crfree(credp);
1554     return afs_convert_code(code);
1555 }
1556
1557
1558 static int
1559 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1560 {
1561     int code;
1562     cred_t *credp = crref();
1563     struct vattr vattr;
1564     const char *name = dp->d_name.name;
1565
1566     /* If afs_symlink returned the vnode, we could instantiate the
1567      * dentry. Since it's not, we drop this one and do a new lookup.
1568      */
1569     d_drop(dp);
1570
1571     VATTR_NULL(&vattr);
1572     AFS_GLOCK();
1573     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1574     AFS_GUNLOCK();
1575     crfree(credp);
1576     return afs_convert_code(code);
1577 }
1578
1579 static int
1580 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1581 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1582 #else
1583 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1584 #endif
1585 {
1586     int code;
1587     cred_t *credp = crref();
1588     struct vcache *tvcp = NULL;
1589     struct vattr vattr;
1590     const char *name = dp->d_name.name;
1591
1592     VATTR_NULL(&vattr);
1593     vattr.va_mask = ATTR_MODE;
1594     vattr.va_mode = mode;
1595     AFS_GLOCK();
1596     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1597
1598     if (tvcp) {
1599         struct inode *ip = AFSTOV(tvcp);
1600
1601         afs_getattr(tvcp, &vattr, credp);
1602         afs_fill_inode(ip, &vattr);
1603
1604 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1605         dp->d_op = &afs_dentry_operations;
1606 #endif
1607         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1608         d_instantiate(dp, ip);
1609     }
1610     AFS_GUNLOCK();
1611
1612     crfree(credp);
1613     return afs_convert_code(code);
1614 }
1615
1616 static int
1617 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1618 {
1619     int code;
1620     cred_t *credp = crref();
1621     const char *name = dp->d_name.name;
1622
1623     /* locking kernel conflicts with glock? */
1624
1625     AFS_GLOCK();
1626     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1627     AFS_GUNLOCK();
1628
1629     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1630      * that failed because a directory is not empty. So, we map
1631      * EEXIST to ENOTEMPTY on linux.
1632      */
1633     if (code == EEXIST) {
1634         code = ENOTEMPTY;
1635     }
1636
1637     if (!code) {
1638         d_drop(dp);
1639     }
1640
1641     crfree(credp);
1642     return afs_convert_code(code);
1643 }
1644
1645
1646 static int
1647 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1648                  struct inode *newip, struct dentry *newdp)
1649 {
1650     int code;
1651     cred_t *credp = crref();
1652     const char *oldname = olddp->d_name.name;
1653     const char *newname = newdp->d_name.name;
1654     struct dentry *rehash = NULL;
1655
1656     /* Prevent any new references during rename operation. */
1657
1658     if (!d_unhashed(newdp)) {
1659         d_drop(newdp);
1660         rehash = newdp;
1661     }
1662
1663 #if defined(D_COUNT_INT)
1664     spin_lock(&olddp->d_lock);
1665     if (olddp->d_count > 1) {
1666         spin_unlock(&olddp->d_lock);
1667         shrink_dcache_parent(olddp);
1668     } else
1669         spin_unlock(&olddp->d_lock);
1670 #else
1671     if (atomic_read(&olddp->d_count) > 1)
1672         shrink_dcache_parent(olddp);
1673 #endif
1674
1675     AFS_GLOCK();
1676     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1677     AFS_GUNLOCK();
1678
1679     if (!code)
1680         olddp->d_time = 0;      /* force to revalidate */
1681
1682     if (rehash)
1683         d_rehash(rehash);
1684
1685     crfree(credp);
1686     return afs_convert_code(code);
1687 }
1688
1689
1690 /* afs_linux_ireadlink 
1691  * Internal readlink which can return link contents to user or kernel space.
1692  * Note that the buffer is NOT supposed to be null-terminated.
1693  */
1694 static int
1695 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1696 {
1697     int code;
1698     cred_t *credp = crref();
1699     struct uio tuio;
1700     struct iovec iov;
1701
1702     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1703     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1704     crfree(credp);
1705
1706     if (!code)
1707         return maxlen - tuio.uio_resid;
1708     else
1709         return afs_convert_code(code);
1710 }
1711
1712 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1713 /* afs_linux_readlink 
1714  * Fill target (which is in user space) with contents of symlink.
1715  */
1716 static int
1717 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1718 {
1719     int code;
1720     struct inode *ip = dp->d_inode;
1721
1722     AFS_GLOCK();
1723     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1724     AFS_GUNLOCK();
1725     return code;
1726 }
1727
1728
1729 /* afs_linux_follow_link
1730  * a file system dependent link following routine.
1731  */
1732 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1733 {
1734     int code;
1735     char *name;
1736
1737     name = kmalloc(PATH_MAX, GFP_NOFS);
1738     if (!name) {
1739         return -EIO;
1740     }
1741
1742     AFS_GLOCK();
1743     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1744     AFS_GUNLOCK();
1745
1746     if (code < 0) {
1747         return code;
1748     }
1749
1750     name[code] = '\0';
1751     nd_set_link(nd, name);
1752     return 0;
1753 }
1754
1755 static void
1756 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1757 {
1758     char *name = nd_get_link(nd);
1759
1760     if (name && !IS_ERR(name))
1761         kfree(name);
1762 }
1763
1764 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1765
1766 /* Populate a page by filling it from the cache file pointed at by cachefp
1767  * (which contains indicated chunk)
1768  * If task is NULL, the page copy occurs syncronously, and the routine
1769  * returns with page still locked. If task is non-NULL, then page copies
1770  * may occur in the background, and the page will be unlocked when it is
1771  * ready for use.
1772  */
1773 static int
1774 afs_linux_read_cache(struct file *cachefp, struct page *page,
1775                      int chunk, struct pagevec *lrupv,
1776                      struct afs_pagecopy_task *task) {
1777     loff_t offset = page_offset(page);
1778     struct inode *cacheinode = cachefp->f_dentry->d_inode;
1779     struct page *newpage, *cachepage;
1780     struct address_space *cachemapping;
1781     int pageindex;
1782     int code = 0;
1783
1784     cachemapping = cacheinode->i_mapping;
1785     newpage = NULL;
1786     cachepage = NULL;
1787
1788     /* If we're trying to read a page that's past the end of the disk
1789      * cache file, then just return a zeroed page */
1790     if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1791         zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1792         SetPageUptodate(page);
1793         if (task)
1794             unlock_page(page);
1795         return 0;
1796     }
1797
1798     /* From our offset, we now need to work out which page in the disk
1799      * file it corresponds to. This will be fun ... */
1800     pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1801
1802     while (cachepage == NULL) {
1803         cachepage = find_get_page(cachemapping, pageindex);
1804         if (!cachepage) {
1805             if (!newpage)
1806                 newpage = page_cache_alloc_cold(cachemapping);
1807             if (!newpage) {
1808                 code = -ENOMEM;
1809                 goto out;
1810             }
1811
1812             code = add_to_page_cache(newpage, cachemapping,
1813                                      pageindex, GFP_KERNEL);
1814             if (code == 0) {
1815                 cachepage = newpage;
1816                 newpage = NULL;
1817
1818                 page_cache_get(cachepage);
1819                 if (!pagevec_add(lrupv, cachepage))
1820                     __pagevec_lru_add_file(lrupv);
1821
1822             } else {
1823                 page_cache_release(newpage);
1824                 newpage = NULL;
1825                 if (code != -EEXIST)
1826                     goto out;
1827             }
1828         } else {
1829             lock_page(cachepage);
1830         }
1831     }
1832
1833     if (!PageUptodate(cachepage)) {
1834         ClearPageError(cachepage);
1835         code = cachemapping->a_ops->readpage(NULL, cachepage);
1836         if (!code && !task) {
1837             wait_on_page_locked(cachepage);
1838         }
1839     } else {
1840         unlock_page(cachepage);
1841     }
1842
1843     if (!code) {
1844         if (PageUptodate(cachepage)) {
1845             copy_highpage(page, cachepage);
1846             flush_dcache_page(page);
1847             SetPageUptodate(page);
1848
1849             if (task)
1850                 unlock_page(page);
1851         } else if (task) {
1852             afs_pagecopy_queue_page(task, cachepage, page);
1853         } else {
1854             code = -EIO;
1855         }
1856     }
1857
1858     if (code && task) {
1859         unlock_page(page);
1860     }
1861
1862 out:
1863     if (cachepage)
1864         page_cache_release(cachepage);
1865
1866     return code;
1867 }
1868
1869 static int inline
1870 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1871 {
1872     loff_t offset = page_offset(pp);
1873     struct inode *ip = FILE_INODE(fp);
1874     struct vcache *avc = VTOAFS(ip);
1875     struct dcache *tdc;
1876     struct file *cacheFp = NULL;
1877     int code;
1878     int dcLocked = 0;
1879     struct pagevec lrupv;
1880
1881     /* Not a UFS cache, don't do anything */
1882     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1883         return 0;
1884
1885     /* Can't do anything if the vcache isn't statd , or if the read
1886      * crosses a chunk boundary.
1887      */
1888     if (!(avc->f.states & CStatd) ||
1889         AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1890         return 0;
1891     }
1892
1893     ObtainWriteLock(&avc->lock, 911);
1894
1895     /* XXX - See if hinting actually makes things faster !!! */
1896
1897     /* See if we have a suitable entry already cached */
1898     tdc = avc->dchint;
1899
1900     if (tdc) {
1901         /* We need to lock xdcache, then dcache, to handle situations where
1902          * the hint is on the free list. However, we can't safely do this
1903          * according to the locking hierarchy. So, use a non blocking lock.
1904          */
1905         ObtainReadLock(&afs_xdcache);
1906         dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1907
1908         if (dcLocked && (tdc->index != NULLIDX)
1909             && !FidCmp(&tdc->f.fid, &avc->f.fid)
1910             && tdc->f.chunk == AFS_CHUNK(offset)
1911             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1912             /* Bonus - the hint was correct */
1913             afs_RefDCache(tdc);
1914         } else {
1915             /* Only destroy the hint if its actually invalid, not if there's
1916              * just been a locking failure */
1917             if (dcLocked) {
1918                 ReleaseReadLock(&tdc->lock);
1919                 avc->dchint = NULL;
1920             }
1921
1922             tdc = NULL;
1923             dcLocked = 0;
1924         }
1925         ReleaseReadLock(&afs_xdcache);
1926     }
1927
1928     /* No hint, or hint is no longer valid - see if we can get something
1929      * directly from the dcache
1930      */
1931     if (!tdc)
1932         tdc = afs_FindDCache(avc, offset);
1933
1934     if (!tdc) {
1935         ReleaseWriteLock(&avc->lock);
1936         return 0;
1937     }
1938
1939     if (!dcLocked)
1940         ObtainReadLock(&tdc->lock);
1941
1942     /* Is the dcache we've been given currently up to date */
1943     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1944         (tdc->dflags & DFFetching)) {
1945         ReleaseWriteLock(&avc->lock);
1946         ReleaseReadLock(&tdc->lock);
1947         afs_PutDCache(tdc);
1948         return 0;
1949     }
1950
1951     /* Update our hint for future abuse */
1952     avc->dchint = tdc;
1953
1954     /* Okay, so we've now got a cache file that is up to date */
1955
1956     /* XXX - I suspect we should be locking the inodes before we use them! */
1957     AFS_GUNLOCK();
1958     cacheFp = afs_linux_raw_open(&tdc->f.inode);
1959     pagevec_init(&lrupv, 0);
1960
1961     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1962
1963     if (pagevec_count(&lrupv))
1964        __pagevec_lru_add_file(&lrupv);
1965
1966     filp_close(cacheFp, NULL);
1967     AFS_GLOCK();
1968
1969     ReleaseReadLock(&tdc->lock);
1970     ReleaseWriteLock(&avc->lock);
1971     afs_PutDCache(tdc);
1972
1973     *codep = code;
1974     return 1;
1975 }
1976
1977 /* afs_linux_readpage
1978  *
1979  * This function is split into two, because prepare_write/begin_write
1980  * require a readpage call which doesn't unlock the resulting page upon
1981  * success.
1982  */
1983 static int
1984 afs_linux_fillpage(struct file *fp, struct page *pp)
1985 {
1986     afs_int32 code;
1987     char *address;
1988     struct uio *auio;
1989     struct iovec *iovecp;
1990     struct inode *ip = FILE_INODE(fp);
1991     afs_int32 cnt = page_count(pp);
1992     struct vcache *avc = VTOAFS(ip);
1993     afs_offs_t offset = page_offset(pp);
1994     cred_t *credp;
1995
1996     AFS_GLOCK();
1997     if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1998         AFS_GUNLOCK();
1999         return code;
2000     }
2001     AFS_GUNLOCK();
2002
2003     credp = crref();
2004     address = kmap(pp);
2005     ClearPageError(pp);
2006
2007     auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2008     iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2009
2010     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2011               AFS_UIOSYS);
2012
2013     AFS_GLOCK();
2014     AFS_DISCON_LOCK();
2015     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2016                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2017                99999);  /* not a possible code value */
2018
2019     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2020         
2021     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2022                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2023                code);
2024     AFS_DISCON_UNLOCK();
2025     AFS_GUNLOCK();
2026     if (!code) {
2027         /* XXX valid for no-cache also?  Check last bits of files... :)
2028          * Cognate code goes in afs_NoCacheFetchProc.  */
2029         if (auio->uio_resid)    /* zero remainder of page */
2030              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2031                     auio->uio_resid);
2032
2033         flush_dcache_page(pp);
2034         SetPageUptodate(pp);
2035     } /* !code */
2036
2037     kunmap(pp);
2038
2039     kfree(auio);
2040     kfree(iovecp);
2041
2042     crfree(credp);
2043     return afs_convert_code(code);
2044 }
2045
2046 static int
2047 afs_linux_prefetch(struct file *fp, struct page *pp)
2048 {
2049     int code = 0;
2050     struct vcache *avc = VTOAFS(FILE_INODE(fp));
2051     afs_offs_t offset = page_offset(pp);
2052
2053     if (AFS_CHUNKOFFSET(offset) == 0) {
2054         struct dcache *tdc;
2055         struct vrequest treq;
2056         cred_t *credp;
2057
2058         credp = crref();
2059         AFS_GLOCK();
2060         code = afs_InitReq(&treq, credp);
2061         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2062             tdc = afs_FindDCache(avc, offset);
2063             if (tdc) {
2064                 if (!(tdc->mflags & DFNextStarted))
2065                     afs_PrefetchChunk(avc, tdc, credp, &treq);
2066                     afs_PutDCache(tdc);
2067             }
2068             ReleaseWriteLock(&avc->lock);
2069         }
2070         AFS_GUNLOCK();
2071         crfree(credp);
2072     }
2073     return afs_convert_code(code);
2074
2075 }
2076
2077 static int
2078 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2079                            struct list_head *page_list, unsigned num_pages)
2080 {
2081     afs_int32 page_ix;
2082     struct uio *auio;
2083     afs_offs_t offset;
2084     struct iovec* iovecp;
2085     struct nocache_read_request *ancr;
2086     struct page *pp;
2087     struct pagevec lrupv;
2088     afs_int32 code = 0;
2089
2090     cred_t *credp;
2091     struct inode *ip = FILE_INODE(fp);
2092     struct vcache *avc = VTOAFS(ip);
2093     afs_int32 base_index = 0;
2094     afs_int32 page_count = 0;
2095     afs_int32 isize;
2096
2097     /* background thread must free: iovecp, auio, ancr */
2098     iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2099
2100     auio = osi_Alloc(sizeof(struct uio));
2101     auio->uio_iov = iovecp;
2102     auio->uio_iovcnt = num_pages;
2103     auio->uio_flag = UIO_READ;
2104     auio->uio_seg = AFS_UIOSYS;
2105     auio->uio_resid = num_pages * PAGE_SIZE;
2106
2107     ancr = osi_Alloc(sizeof(struct nocache_read_request));
2108     ancr->auio = auio;
2109     ancr->offset = auio->uio_offset;
2110     ancr->length = auio->uio_resid;
2111
2112     pagevec_init(&lrupv, 0);
2113
2114     for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2115
2116         if(list_empty(page_list))
2117             break;
2118
2119         pp = list_entry(page_list->prev, struct page, lru);
2120         /* If we allocate a page and don't remove it from page_list,
2121          * the page cache gets upset. */
2122         list_del(&pp->lru);
2123         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
2124         if(pp->index > isize) {
2125             if(PageLocked(pp))
2126                 unlock_page(pp);
2127             continue;
2128         }
2129
2130         if(page_ix == 0) {
2131             offset = page_offset(pp);
2132             ancr->offset = auio->uio_offset = offset;
2133             base_index = pp->index;
2134         }
2135         iovecp[page_ix].iov_len = PAGE_SIZE;
2136         code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2137         if(base_index != pp->index) {
2138             if(PageLocked(pp))
2139                  unlock_page(pp);
2140             page_cache_release(pp);
2141             iovecp[page_ix].iov_base = (void *) 0;
2142             base_index++;
2143             ancr->length -= PAGE_SIZE;
2144             continue;
2145         }
2146         base_index++;
2147         if(code) {
2148             if(PageLocked(pp))
2149                 unlock_page(pp);
2150             page_cache_release(pp);
2151             iovecp[page_ix].iov_base = (void *) 0;
2152         } else {
2153             page_count++;
2154             if(!PageLocked(pp)) {
2155                 lock_page(pp);
2156             }
2157
2158             /* increment page refcount--our original design assumed
2159              * that locking it would effectively pin it;  protect
2160              * ourselves from the possiblity that this assumption is
2161              * is faulty, at low cost (provided we do not fail to
2162              * do the corresponding decref on the other side) */
2163             get_page(pp);
2164
2165             /* save the page for background map */
2166             iovecp[page_ix].iov_base = (void*) pp;
2167
2168             /* and put it on the LRU cache */
2169             if (!pagevec_add(&lrupv, pp))
2170                 __pagevec_lru_add_file(&lrupv);
2171         }
2172     }
2173
2174     /* If there were useful pages in the page list, make sure all pages
2175      * are in the LRU cache, then schedule the read */
2176     if(page_count) {
2177         if (pagevec_count(&lrupv))
2178             __pagevec_lru_add_file(&lrupv);
2179         credp = crref();
2180         code = afs_ReadNoCache(avc, ancr, credp);
2181         crfree(credp);
2182     } else {
2183         /* If there is nothing for the background thread to handle,
2184          * it won't be freeing the things that we never gave it */
2185         osi_Free(iovecp, num_pages * sizeof(struct iovec));
2186         osi_Free(auio, sizeof(struct uio));
2187         osi_Free(ancr, sizeof(struct nocache_read_request));
2188     }
2189     /* we do not flush, release, or unmap pages--that will be
2190      * done for us by the background thread as each page comes in
2191      * from the fileserver */
2192     return afs_convert_code(code);
2193 }
2194
2195
2196 static int
2197 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2198 {
2199     cred_t *credp = NULL;
2200     struct uio *auio;
2201     struct iovec *iovecp;
2202     struct nocache_read_request *ancr;
2203     int code;
2204
2205     /*
2206      * Special case: if page is at or past end of file, just zero it and set
2207      * it as up to date.
2208      */
2209     if (page_offset(pp) >=  i_size_read(fp->f_mapping->host)) {
2210         zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
2211         SetPageUptodate(pp);
2212         unlock_page(pp);
2213         return 0;
2214     }
2215
2216     ClearPageError(pp);
2217
2218     /* receiver frees */
2219     auio = osi_Alloc(sizeof(struct uio));
2220     iovecp = osi_Alloc(sizeof(struct iovec));
2221
2222     /* address can be NULL, because we overwrite it with 'pp', below */
2223     setup_uio(auio, iovecp, NULL, page_offset(pp),
2224               PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2225
2226     /* save the page for background map */
2227     get_page(pp); /* see above */
2228     auio->uio_iov->iov_base = (void*) pp;
2229     /* the background thread will free this */
2230     ancr = osi_Alloc(sizeof(struct nocache_read_request));
2231     ancr->auio = auio;
2232     ancr->offset = page_offset(pp);
2233     ancr->length = PAGE_SIZE;
2234
2235     credp = crref();
2236     code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2237     crfree(credp);
2238
2239     return afs_convert_code(code);
2240 }
2241
2242 static inline int
2243 afs_linux_can_bypass(struct inode *ip) {
2244
2245     switch(cache_bypass_strategy) {
2246         case NEVER_BYPASS_CACHE:
2247             return 0;
2248         case ALWAYS_BYPASS_CACHE:
2249             return 1;
2250         case LARGE_FILES_BYPASS_CACHE:
2251             if (i_size_read(ip) > cache_bypass_threshold)
2252                 return 1;
2253         default:
2254             return 0;
2255      }
2256 }
2257
2258 /* Check if a file is permitted to bypass the cache by policy, and modify
2259  * the cache bypass state recorded for that file */
2260
2261 static inline int
2262 afs_linux_bypass_check(struct inode *ip) {
2263     cred_t* credp;
2264
2265     int bypass = afs_linux_can_bypass(ip);
2266
2267     credp = crref();
2268     trydo_cache_transition(VTOAFS(ip), credp, bypass);
2269     crfree(credp);
2270
2271     return bypass;
2272 }
2273
2274
2275 static int
2276 afs_linux_readpage(struct file *fp, struct page *pp)
2277 {
2278     int code;
2279
2280     if (afs_linux_bypass_check(FILE_INODE(fp))) {
2281         code = afs_linux_bypass_readpage(fp, pp);
2282     } else {
2283         code = afs_linux_fillpage(fp, pp);
2284         if (!code)
2285             code = afs_linux_prefetch(fp, pp);
2286         unlock_page(pp);
2287     }
2288
2289     return code;
2290 }
2291
2292 /* Readpages reads a number of pages for a particular file. We use
2293  * this to optimise the reading, by limiting the number of times upon which
2294  * we have to lookup, lock and open vcaches and dcaches
2295  */
2296
2297 static int
2298 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2299                     struct list_head *page_list, unsigned int num_pages)
2300 {
2301     struct inode *inode = mapping->host;
2302     struct vcache *avc = VTOAFS(inode);
2303     struct dcache *tdc;
2304     struct file *cacheFp = NULL;
2305     int code;
2306     unsigned int page_idx;
2307     loff_t offset;
2308     struct pagevec lrupv;
2309     struct afs_pagecopy_task *task;
2310
2311     if (afs_linux_bypass_check(inode))
2312         return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2313
2314     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2315         return 0;
2316
2317     AFS_GLOCK();
2318     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2319         AFS_GUNLOCK();
2320         return code;
2321     }
2322
2323     ObtainWriteLock(&avc->lock, 912);
2324     AFS_GUNLOCK();
2325
2326     task = afs_pagecopy_init_task();
2327
2328     tdc = NULL;
2329     pagevec_init(&lrupv, 0);
2330     for (page_idx = 0; page_idx < num_pages; page_idx++) {
2331         struct page *page = list_entry(page_list->prev, struct page, lru);
2332         list_del(&page->lru);
2333         offset = page_offset(page);
2334
2335         if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2336             AFS_GLOCK();
2337             ReleaseReadLock(&tdc->lock);
2338             afs_PutDCache(tdc);
2339             AFS_GUNLOCK();
2340             tdc = NULL;
2341             if (cacheFp)
2342                 filp_close(cacheFp, NULL);
2343         }
2344
2345         if (!tdc) {
2346             AFS_GLOCK();
2347             if ((tdc = afs_FindDCache(avc, offset))) {
2348                 ObtainReadLock(&tdc->lock);
2349                 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2350                     (tdc->dflags & DFFetching)) {
2351                     ReleaseReadLock(&tdc->lock);
2352                     afs_PutDCache(tdc);
2353                     tdc = NULL;
2354                 }
2355             }
2356             AFS_GUNLOCK();
2357             if (tdc)
2358                 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2359         }
2360
2361         if (tdc && !add_to_page_cache(page, mapping, page->index,
2362                                       GFP_KERNEL)) {
2363             page_cache_get(page);
2364             if (!pagevec_add(&lrupv, page))
2365                 __pagevec_lru_add_file(&lrupv);
2366
2367             afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2368         }
2369         page_cache_release(page);
2370     }
2371     if (pagevec_count(&lrupv))
2372        __pagevec_lru_add_file(&lrupv);
2373
2374     if (tdc)
2375         filp_close(cacheFp, NULL);
2376
2377     afs_pagecopy_put_task(task);
2378
2379     AFS_GLOCK();
2380     if (tdc) {
2381         ReleaseReadLock(&tdc->lock);
2382         afs_PutDCache(tdc);
2383     }
2384
2385     ReleaseWriteLock(&avc->lock);
2386     AFS_GUNLOCK();
2387     return 0;
2388 }
2389
2390 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2391  * locked */
2392 static inline int
2393 afs_linux_prepare_writeback(struct vcache *avc) {
2394     if (avc->f.states & CPageWrite) {
2395         return AOP_WRITEPAGE_ACTIVATE;
2396     }
2397     avc->f.states |= CPageWrite;
2398     return 0;
2399 }
2400
2401 static inline int
2402 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2403     struct vrequest treq;
2404     int code = 0;
2405
2406     if (!afs_InitReq(&treq, credp))
2407         code = afs_DoPartialWrite(avc, &treq);
2408
2409     return afs_convert_code(code);
2410 }
2411
2412 static inline void
2413 afs_linux_complete_writeback(struct vcache *avc) {
2414     avc->f.states &= ~CPageWrite;
2415 }
2416
2417 /* Writeback a given page syncronously. Called with no AFS locks held */
2418 static int
2419 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2420                          unsigned long offset, unsigned int count,
2421                          cred_t *credp)
2422 {
2423     struct vcache *vcp = VTOAFS(ip);
2424     char *buffer;
2425     afs_offs_t base;
2426     int code = 0;
2427     struct uio tuio;
2428     struct iovec iovec;
2429     int f_flags = 0;
2430
2431     buffer = kmap(pp) + offset;
2432     base = page_offset(pp) + offset;
2433
2434     AFS_GLOCK();
2435     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2436                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2437                ICL_TYPE_INT32, 99999);
2438
2439     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2440
2441     code = afs_write(vcp, &tuio, f_flags, credp, 0);
2442
2443     i_size_write(ip, vcp->f.m.Length);
2444     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2445
2446     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2447
2448     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2449                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2450                ICL_TYPE_INT32, code);
2451
2452     AFS_GUNLOCK();
2453     kunmap(pp);
2454
2455     return code;
2456 }
2457
2458 static int
2459 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2460                          unsigned long offset, unsigned int count)
2461 {
2462     int code;
2463     int code1 = 0;
2464     struct vcache *vcp = VTOAFS(ip);
2465     cred_t *credp;
2466
2467     /* Catch recursive writeback. This occurs if the kernel decides
2468      * writeback is required whilst we are writing to the cache, or
2469      * flushing to the server. When we're running syncronously (as
2470      * opposed to from writepage) we can't actually do anything about
2471      * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2472      */
2473     AFS_GLOCK();
2474     ObtainWriteLock(&vcp->lock, 532);
2475     afs_linux_prepare_writeback(vcp);
2476     ReleaseWriteLock(&vcp->lock);
2477     AFS_GUNLOCK();
2478
2479     credp = crref();
2480     code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2481
2482     AFS_GLOCK();
2483     ObtainWriteLock(&vcp->lock, 533);
2484     if (code > 0)
2485         code1 = afs_linux_dopartialwrite(vcp, credp);
2486     afs_linux_complete_writeback(vcp);
2487     ReleaseWriteLock(&vcp->lock);
2488     AFS_GUNLOCK();
2489     crfree(credp);
2490
2491     if (code1)
2492         return code1;
2493
2494     return code;
2495 }
2496
2497 static int
2498 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2499 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2500 #else
2501 afs_linux_writepage(struct page *pp)
2502 #endif
2503 {
2504     struct address_space *mapping = pp->mapping;
2505     struct inode *inode;
2506     struct vcache *vcp;
2507     cred_t *credp;
2508     unsigned int to = PAGE_CACHE_SIZE;
2509     loff_t isize;
2510     int code = 0;
2511     int code1 = 0;
2512
2513     if (PageReclaim(pp)) {
2514         return AOP_WRITEPAGE_ACTIVATE;
2515         /* XXX - Do we need to redirty the page here? */
2516     }
2517
2518     page_cache_get(pp);
2519
2520     inode = mapping->host;
2521     vcp = VTOAFS(inode);
2522     isize = i_size_read(inode);
2523
2524     /* Don't defeat an earlier truncate */
2525     if (page_offset(pp) > isize) {
2526         set_page_writeback(pp);
2527         unlock_page(pp);
2528         goto done;
2529     }
2530
2531     AFS_GLOCK();
2532     ObtainWriteLock(&vcp->lock, 537);
2533     code = afs_linux_prepare_writeback(vcp);
2534     if (code == AOP_WRITEPAGE_ACTIVATE) {
2535         /* WRITEPAGE_ACTIVATE is the only return value that permits us
2536          * to return with the page still locked */
2537         ReleaseWriteLock(&vcp->lock);
2538         AFS_GUNLOCK();
2539         return code;
2540     }
2541
2542     /* Grab the creds structure currently held in the vnode, and
2543      * get a reference to it, in case it goes away ... */
2544     credp = vcp->cred;
2545     if (credp)
2546         crhold(credp);
2547     else
2548         credp = crref();
2549     ReleaseWriteLock(&vcp->lock);
2550     AFS_GUNLOCK();
2551
2552     set_page_writeback(pp);
2553
2554     SetPageUptodate(pp);
2555
2556     /* We can unlock the page here, because it's protected by the
2557      * page_writeback flag. This should make us less vulnerable to
2558      * deadlocking in afs_write and afs_DoPartialWrite
2559      */
2560     unlock_page(pp);
2561
2562     /* If this is the final page, then just write the number of bytes that
2563      * are actually in it */
2564     if ((isize - page_offset(pp)) < to )
2565         to = isize - page_offset(pp);
2566
2567     code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2568
2569     AFS_GLOCK();
2570     ObtainWriteLock(&vcp->lock, 538);
2571
2572     /* As much as we might like to ignore a file server error here,
2573      * and just try again when we close(), unfortunately StoreAllSegments
2574      * will invalidate our chunks if the server returns a permanent error,
2575      * so we need to at least try and get that error back to the user
2576      */
2577     if (code == to)
2578         code1 = afs_linux_dopartialwrite(vcp, credp);
2579
2580     afs_linux_complete_writeback(vcp);
2581     ReleaseWriteLock(&vcp->lock);
2582     crfree(credp);
2583     AFS_GUNLOCK();
2584
2585 done:
2586     end_page_writeback(pp);
2587     page_cache_release(pp);
2588
2589     if (code1)
2590         return code1;
2591
2592     if (code == to)
2593         return 0;
2594
2595     return code;
2596 }
2597
2598 /* afs_linux_permission
2599  * Check access rights - returns error if can't check or permission denied.
2600  */
2601 static int
2602 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2603 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2604 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2605 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2606 #else
2607 afs_linux_permission(struct inode *ip, int mode)
2608 #endif
2609 {
2610     int code;
2611     cred_t *credp;
2612     int tmp = 0;
2613
2614     /* Check for RCU path walking */
2615 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2616     if (flags & IPERM_FLAG_RCU)
2617        return -ECHILD;
2618 #elif defined(MAY_NOT_BLOCK)
2619     if (mode & MAY_NOT_BLOCK)
2620        return -ECHILD;
2621 #endif
2622
2623     credp = crref();
2624     AFS_GLOCK();
2625     if (mode & MAY_EXEC)
2626         tmp |= VEXEC;
2627     if (mode & MAY_READ)
2628         tmp |= VREAD;
2629     if (mode & MAY_WRITE)
2630         tmp |= VWRITE;
2631     code = afs_access(VTOAFS(ip), tmp, credp);
2632
2633     AFS_GUNLOCK();
2634     crfree(credp);
2635     return afs_convert_code(code);
2636 }
2637
2638 static int
2639 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2640                        unsigned to)
2641 {
2642     int code;
2643     struct inode *inode = FILE_INODE(file);
2644     loff_t pagebase = page_offset(page);
2645
2646     if (i_size_read(inode) < (pagebase + offset))
2647         i_size_write(inode, pagebase + offset);
2648
2649     if (PageChecked(page)) {
2650         SetPageUptodate(page);
2651         ClearPageChecked(page);
2652     }
2653
2654     code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2655
2656     return code;
2657 }
2658
2659 static int
2660 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2661                         unsigned to)
2662 {
2663
2664     /* http://kerneltrap.org/node/4941 details the expected behaviour of
2665      * prepare_write. Essentially, if the page exists within the file,
2666      * and is not being fully written, then we should populate it.
2667      */
2668
2669     if (!PageUptodate(page)) {
2670         loff_t pagebase = page_offset(page);
2671         loff_t isize = i_size_read(page->mapping->host);
2672
2673         /* Is the location we are writing to beyond the end of the file? */
2674         if (pagebase >= isize ||
2675             ((from == 0) && (pagebase + to) >= isize)) {
2676             zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2677             SetPageChecked(page);
2678         /* Are we we writing a full page */
2679         } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2680             SetPageChecked(page);
2681         /* Is the page readable, if it's wronly, we don't care, because we're
2682          * not actually going to read from it ... */
2683         } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2684             /* We don't care if fillpage fails, because if it does the page
2685              * won't be marked as up to date
2686              */
2687             afs_linux_fillpage(file, page);
2688         }
2689     }
2690     return 0;
2691 }
2692
2693 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2694 static int
2695 afs_linux_write_end(struct file *file, struct address_space *mapping,
2696                                 loff_t pos, unsigned len, unsigned copied,
2697                                 struct page *page, void *fsdata)
2698 {
2699     int code;
2700     unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2701
2702     code = afs_linux_commit_write(file, page, from, from + len);
2703
2704     unlock_page(page);
2705     page_cache_release(page);
2706     return code;
2707 }
2708
2709 static int
2710 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2711                                 loff_t pos, unsigned len, unsigned flags,
2712                                 struct page **pagep, void **fsdata)
2713 {
2714     struct page *page;
2715     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2716     unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2717     int code;
2718
2719     page = grab_cache_page_write_begin(mapping, index, flags);
2720     *pagep = page;
2721
2722     code = afs_linux_prepare_write(file, page, from, from + len);
2723     if (code) {
2724         unlock_page(page);
2725         page_cache_release(page);
2726     }
2727
2728     return code;
2729 }
2730 #endif
2731
2732 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2733 static void *
2734 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2735 {
2736     struct dentry **dpp;
2737     struct dentry *target;
2738
2739     if (current->total_link_count > 0) {
2740         /* avoid symlink resolution limits when resolving; we cannot contribute to
2741          * an infinite symlink loop */
2742         /* only do this for follow_link when total_link_count is positive to be
2743          * on the safe side; there is at least one code path in the Linux
2744          * kernel where it seems like it may be possible to get here without
2745          * total_link_count getting incremented. it is not clear on how that
2746          * path is actually reached, but guard against it just to be safe */
2747         current->total_link_count--;
2748     }
2749
2750     target = canonical_dentry(dentry->d_inode);
2751
2752 # ifdef STRUCT_NAMEIDATA_HAS_PATH
2753     dpp = &nd->path.dentry;
2754 # else
2755     dpp = &nd->dentry;
2756 # endif
2757
2758     dput(*dpp);
2759
2760     if (target) {
2761         *dpp = target;
2762     } else {
2763         *dpp = dget(dentry);
2764     }
2765
2766     nd->last_type = LAST_BIND;
2767
2768     return NULL;
2769 }
2770 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
2771
2772
2773 static struct inode_operations afs_file_iops = {
2774   .permission =         afs_linux_permission,
2775   .getattr =            afs_linux_getattr,
2776   .setattr =            afs_notify_change,
2777 };
2778
2779 static struct address_space_operations afs_file_aops = {
2780   .readpage =           afs_linux_readpage,
2781   .readpages =          afs_linux_readpages,
2782   .writepage =          afs_linux_writepage,
2783 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2784   .write_begin =        afs_linux_write_begin,
2785   .write_end =          afs_linux_write_end,
2786 #else
2787   .commit_write =       afs_linux_commit_write,
2788   .prepare_write =      afs_linux_prepare_write,
2789 #endif
2790 };
2791
2792
2793 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2794  * by what sort of operation is allowed.....
2795  */
2796
2797 static struct inode_operations afs_dir_iops = {
2798   .setattr =            afs_notify_change,
2799   .create =             afs_linux_create,
2800   .lookup =             afs_linux_lookup,
2801   .link =               afs_linux_link,
2802   .unlink =             afs_linux_unlink,
2803   .symlink =            afs_linux_symlink,
2804   .mkdir =              afs_linux_mkdir,
2805   .rmdir =              afs_linux_rmdir,
2806   .rename =             afs_linux_rename,
2807   .getattr =            afs_linux_getattr,
2808   .permission =         afs_linux_permission,
2809 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2810   .follow_link =        afs_linux_dir_follow_link,
2811 #endif
2812 };
2813
2814 /* We really need a separate symlink set of ops, since do_follow_link()
2815  * determines if it _is_ a link by checking if the follow_link op is set.
2816  */
2817 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2818 static int
2819 afs_symlink_filler(struct file *file, struct page *page)
2820 {
2821     struct inode *ip = (struct inode *)page->mapping->host;
2822     char *p = (char *)kmap(page);
2823     int code;
2824
2825     AFS_GLOCK();
2826     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2827     AFS_GUNLOCK();
2828
2829     if (code < 0)
2830         goto fail;
2831     p[code] = '\0';             /* null terminate? */
2832
2833     SetPageUptodate(page);
2834     kunmap(page);
2835     unlock_page(page);
2836     return 0;
2837
2838   fail:
2839     SetPageError(page);
2840     kunmap(page);
2841     unlock_page(page);
2842     return code;
2843 }
2844
2845 static struct address_space_operations afs_symlink_aops = {
2846   .readpage =   afs_symlink_filler
2847 };
2848 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2849
2850 static struct inode_operations afs_symlink_iops = {
2851 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2852   .readlink =           page_readlink,
2853 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2854   .follow_link =        page_follow_link,
2855 # else
2856   .follow_link =        page_follow_link_light,
2857   .put_link =           page_put_link,
2858 # endif
2859 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2860   .readlink =           afs_linux_readlink,
2861   .follow_link =        afs_linux_follow_link,
2862   .put_link =           afs_linux_put_link,
2863 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2864   .setattr =            afs_notify_change,
2865 };
2866
2867 void
2868 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2869 {
2870         
2871     if (vattr)
2872         vattr2inode(ip, vattr);
2873
2874     ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2875 /* Reset ops if symlink or directory. */
2876     if (S_ISREG(ip->i_mode)) {
2877         ip->i_op = &afs_file_iops;
2878         ip->i_fop = &afs_file_fops;
2879         ip->i_data.a_ops = &afs_file_aops;
2880
2881     } else if (S_ISDIR(ip->i_mode)) {
2882         ip->i_op = &afs_dir_iops;
2883         ip->i_fop = &afs_dir_fops;
2884
2885     } else if (S_ISLNK(ip->i_mode)) {
2886         ip->i_op = &afs_symlink_iops;
2887 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2888         ip->i_data.a_ops = &afs_symlink_aops;
2889         ip->i_mapping = &ip->i_data;
2890 #endif
2891     }
2892
2893 }