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