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