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