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