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