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