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