a9739dbce9dcff789c18c7f580fe8060234a4726
[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         code = afs_CopyOutAttrs(vcp, &vattr);
791     } else {
792         credp = crref();
793         code = afs_getattr(vcp, &vattr, credp);
794         crfree(credp);
795     }
796     if (!code)
797         afs_fill_inode(AFSTOV(vcp), &vattr);
798
799     AFS_GUNLOCK();
800     afs_maybe_unlock_kernel();
801
802     return afs_convert_code(code);
803 }
804
805 static int
806 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
807 {
808         int err = afs_linux_revalidate(dentry);
809         if (!err) {
810                 generic_fillattr(dentry->d_inode, stat);
811 }
812         return err;
813 }
814
815 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
816  * In kernels 2.2.10 and above, we are passed an additional flags var which
817  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
818  * we are advised to follow the entry if it is a link or to make sure that 
819  * it is a directory. But since the kernel itself checks these possibilities
820  * later on, we shouldn't have to do it until later. Perhaps in the future..
821  */
822 static int
823 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
824 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
825 #else
826 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
827 #endif
828 {
829     struct vattr vattr;
830     cred_t *credp = NULL;
831     struct vcache *vcp, *pvcp, *tvc = NULL;
832     int valid;
833     struct afs_fakestat_state fakestate;
834
835     afs_maybe_lock_kernel();
836     AFS_GLOCK();
837     afs_InitFakeStat(&fakestate);
838
839     if (dp->d_inode) {
840
841         vcp = VTOAFS(dp->d_inode);
842         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
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         /* If the parent's DataVersion has changed or the vnode
886          * is longer valid, we need to do a full lookup.  VerifyVCache
887          * isn't enough since the vnode may have been renamed.
888          */
889
890         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
891
892             credp = crref();
893             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
894             if (!tvc || tvc != vcp)
895                 goto bad_dentry;
896
897             if (afs_getattr(vcp, &vattr, credp))
898                 goto bad_dentry;
899
900             vattr2inode(AFSTOV(vcp), &vattr);
901             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
902         }
903
904         /* should we always update the attributes at this point? */
905         /* unlikely--the vcache entry hasn't changed */
906
907     } else {
908 #ifdef notyet
909         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
910         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
911             goto bad_dentry;
912 #endif
913
914         /* No change in parent's DataVersion so this negative
915          * lookup is still valid.  BUT, if a server is down a
916          * negative lookup can result so there should be a
917          * liftime as well.  For now, always expire.
918          */
919
920         goto bad_dentry;
921     }
922
923   good_dentry:
924     valid = 1;
925
926   done:
927     /* Clean up */
928     if (tvc)
929         afs_PutVCache(tvc);
930     afs_PutFakeStat(&fakestate);
931     AFS_GUNLOCK();
932     if (credp)
933         crfree(credp);
934
935     if (!valid) {
936         shrink_dcache_parent(dp);
937         d_drop(dp);
938     }
939     afs_maybe_unlock_kernel();
940     return valid;
941
942   bad_dentry:
943     if (have_submounts(dp))
944         valid = 1;
945     else 
946         valid = 0;
947     goto done;
948 }
949
950 static void
951 afs_dentry_iput(struct dentry *dp, struct inode *ip)
952 {
953     struct vcache *vcp = VTOAFS(ip);
954
955     AFS_GLOCK();
956     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
957         (void) afs_InactiveVCache(vcp, NULL);
958     }
959     AFS_GUNLOCK();
960     afs_linux_clear_nfsfs_renamed(dp);
961
962     iput(ip);
963 }
964
965 static int
966 afs_dentry_delete(struct dentry *dp)
967 {
968     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
969         return 1;               /* bad inode? */
970
971     return 0;
972 }
973
974 struct dentry_operations afs_dentry_operations = {
975   .d_revalidate =       afs_linux_dentry_revalidate,
976   .d_delete =           afs_dentry_delete,
977   .d_iput =             afs_dentry_iput,
978 };
979
980 /**********************************************************************
981  * AFS Linux inode operations
982  **********************************************************************/
983
984 /* afs_linux_create
985  *
986  * Merely need to set enough of vattr to get us through the create. Note
987  * that the higher level code (open_namei) will take care of any tuncation
988  * explicitly. Exclusive open is also taken care of in open_namei.
989  *
990  * name is in kernel space at this point.
991  */
992 static int
993 #ifdef IOP_CREATE_TAKES_NAMEIDATA
994 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
995                  struct nameidata *nd)
996 #else
997 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
998 #endif
999 {
1000     struct vattr vattr;
1001     cred_t *credp = crref();
1002     const char *name = dp->d_name.name;
1003     struct vcache *vcp;
1004     int code;
1005
1006     VATTR_NULL(&vattr);
1007     vattr.va_mode = mode;
1008     vattr.va_type = mode & S_IFMT;
1009
1010     afs_maybe_lock_kernel();
1011     AFS_GLOCK();
1012     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1013                       &vcp, credp);
1014
1015     if (!code) {
1016         struct inode *ip = AFSTOV(vcp);
1017
1018         afs_getattr(vcp, &vattr, credp);
1019         afs_fill_inode(ip, &vattr);
1020         insert_inode_hash(ip);
1021         dp->d_op = &afs_dentry_operations;
1022         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1023         d_instantiate(dp, ip);
1024     }
1025     AFS_GUNLOCK();
1026
1027     afs_maybe_unlock_kernel();
1028     crfree(credp);
1029     return afs_convert_code(code);
1030 }
1031
1032 /* afs_linux_lookup */
1033 static struct dentry *
1034 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1035 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1036                  struct nameidata *nd)
1037 #else
1038 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1039 #endif
1040 {
1041     cred_t *credp = crref();
1042     struct vcache *vcp = NULL;
1043     const char *comp = dp->d_name.name;
1044     struct inode *ip = NULL;
1045     struct dentry *newdp = NULL;
1046     int code;
1047
1048     afs_maybe_lock_kernel();
1049     AFS_GLOCK();
1050     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1051     
1052     if (vcp) {
1053         struct vattr vattr;
1054
1055         ip = AFSTOV(vcp);
1056         afs_getattr(vcp, &vattr, credp);
1057         afs_fill_inode(ip, &vattr);
1058         if (hlist_unhashed(&ip->i_hash))
1059             insert_inode_hash(ip);
1060     }
1061     dp->d_op = &afs_dentry_operations;
1062     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1063     AFS_GUNLOCK();
1064
1065     if (ip && S_ISDIR(ip->i_mode)) {
1066         struct dentry *alias;
1067
1068         /* Try to invalidate an existing alias in favor of our new one */
1069         alias = d_find_alias(ip);
1070         /* But not if it's disconnected; then we want d_splice_alias below */
1071         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1072             if (d_invalidate(alias) == 0) {
1073                 dput(alias);
1074             } else {
1075                 iput(ip);
1076                 unlock_kernel();
1077                 crfree(credp);
1078                 return alias;
1079             }
1080         }
1081     }
1082     newdp = d_splice_alias(ip, dp);
1083
1084     afs_maybe_unlock_kernel();
1085     crfree(credp);
1086
1087     /* It's ok for the file to not be found. That's noted by the caller by
1088      * seeing that the dp->d_inode field is NULL.
1089      */
1090     if (!code || code == ENOENT)
1091         return newdp;
1092     else 
1093         return ERR_PTR(afs_convert_code(code));
1094 }
1095
1096 static int
1097 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1098 {
1099     int code;
1100     cred_t *credp = crref();
1101     const char *name = newdp->d_name.name;
1102     struct inode *oldip = olddp->d_inode;
1103
1104     /* If afs_link returned the vnode, we could instantiate the
1105      * dentry. Since it's not, we drop this one and do a new lookup.
1106      */
1107     d_drop(newdp);
1108
1109     AFS_GLOCK();
1110     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1111
1112     AFS_GUNLOCK();
1113     crfree(credp);
1114     return afs_convert_code(code);
1115 }
1116
1117 static int
1118 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1119 {
1120     int code = EBUSY;
1121     cred_t *credp = crref();
1122     const char *name = dp->d_name.name;
1123     struct vcache *tvc = VTOAFS(dp->d_inode);
1124
1125     afs_maybe_lock_kernel();
1126     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1127                                 && !(tvc->f.states & CUnlinked)) {
1128         struct dentry *__dp;
1129         char *__name;
1130
1131         __dp = NULL;
1132         __name = NULL;
1133         do {
1134             dput(__dp);
1135
1136             AFS_GLOCK();
1137             if (__name)
1138                 osi_FreeSmallSpace(__name);
1139             __name = afs_newname();
1140             AFS_GUNLOCK();
1141
1142             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1143                 
1144             if (IS_ERR(__dp))
1145                 goto out;
1146         } while (__dp->d_inode != NULL);
1147
1148         AFS_GLOCK();
1149         code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1150         if (!code) {
1151             tvc->mvid = (void *) __name;
1152             crhold(credp);
1153             if (tvc->uncred) {
1154                 crfree(tvc->uncred);
1155             }
1156             tvc->uncred = credp;
1157             tvc->f.states |= CUnlinked;
1158             afs_linux_set_nfsfs_renamed(dp);
1159         } else {
1160             osi_FreeSmallSpace(__name); 
1161         }
1162         AFS_GUNLOCK();
1163
1164         if (!code) {
1165             __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1166             d_move(dp, __dp);
1167         }
1168         dput(__dp);
1169
1170         goto out;
1171     }
1172
1173     AFS_GLOCK();
1174     code = afs_remove(VTOAFS(dip), (char *)name, credp);
1175     AFS_GUNLOCK();
1176     if (!code)
1177         d_drop(dp);
1178 out:
1179     afs_maybe_unlock_kernel();
1180     crfree(credp);
1181     return afs_convert_code(code);
1182 }
1183
1184
1185 static int
1186 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1187 {
1188     int code;
1189     cred_t *credp = crref();
1190     struct vattr vattr;
1191     const char *name = dp->d_name.name;
1192
1193     /* If afs_symlink returned the vnode, we could instantiate the
1194      * dentry. Since it's not, we drop this one and do a new lookup.
1195      */
1196     d_drop(dp);
1197
1198     VATTR_NULL(&vattr);
1199     AFS_GLOCK();
1200     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1201     AFS_GUNLOCK();
1202     crfree(credp);
1203     return afs_convert_code(code);
1204 }
1205
1206 static int
1207 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1208 {
1209     int code;
1210     cred_t *credp = crref();
1211     struct vcache *tvcp = NULL;
1212     struct vattr vattr;
1213     const char *name = dp->d_name.name;
1214
1215     afs_maybe_lock_kernel();
1216     VATTR_NULL(&vattr);
1217     vattr.va_mask = ATTR_MODE;
1218     vattr.va_mode = mode;
1219     AFS_GLOCK();
1220     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1221
1222     if (tvcp) {
1223         struct inode *ip = AFSTOV(tvcp);
1224
1225         afs_getattr(tvcp, &vattr, credp);
1226         afs_fill_inode(ip, &vattr);
1227
1228         dp->d_op = &afs_dentry_operations;
1229         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1230         d_instantiate(dp, ip);
1231     }
1232     AFS_GUNLOCK();
1233
1234     afs_maybe_unlock_kernel();
1235     crfree(credp);
1236     return afs_convert_code(code);
1237 }
1238
1239 static int
1240 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1241 {
1242     int code;
1243     cred_t *credp = crref();
1244     const char *name = dp->d_name.name;
1245
1246     /* locking kernel conflicts with glock? */
1247
1248     AFS_GLOCK();
1249     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1250     AFS_GUNLOCK();
1251
1252     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1253      * that failed because a directory is not empty. So, we map
1254      * EEXIST to ENOTEMPTY on linux.
1255      */
1256     if (code == EEXIST) {
1257         code = ENOTEMPTY;
1258     }
1259
1260     if (!code) {
1261         d_drop(dp);
1262     }
1263
1264     crfree(credp);
1265     return afs_convert_code(code);
1266 }
1267
1268
1269 static int
1270 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1271                  struct inode *newip, struct dentry *newdp)
1272 {
1273     int code;
1274     cred_t *credp = crref();
1275     const char *oldname = olddp->d_name.name;
1276     const char *newname = newdp->d_name.name;
1277     struct dentry *rehash = NULL;
1278
1279     /* Prevent any new references during rename operation. */
1280     afs_maybe_lock_kernel();
1281
1282     if (!d_unhashed(newdp)) {
1283         d_drop(newdp);
1284         rehash = newdp;
1285     }
1286
1287     if (atomic_read(&olddp->d_count) > 1)
1288         shrink_dcache_parent(olddp);
1289
1290     AFS_GLOCK();
1291     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1292     AFS_GUNLOCK();
1293
1294     if (!code)
1295         olddp->d_time = 0;      /* force to revalidate */
1296
1297     if (rehash)
1298         d_rehash(rehash);
1299
1300     afs_maybe_unlock_kernel();
1301
1302     crfree(credp);
1303     return afs_convert_code(code);
1304 }
1305
1306
1307 /* afs_linux_ireadlink 
1308  * Internal readlink which can return link contents to user or kernel space.
1309  * Note that the buffer is NOT supposed to be null-terminated.
1310  */
1311 static int
1312 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1313 {
1314     int code;
1315     cred_t *credp = crref();
1316     uio_t tuio;
1317     struct iovec iov;
1318
1319     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1320     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1321     crfree(credp);
1322
1323     if (!code)
1324         return maxlen - tuio.uio_resid;
1325     else
1326         return afs_convert_code(code);
1327 }
1328
1329 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1330 /* afs_linux_readlink 
1331  * Fill target (which is in user space) with contents of symlink.
1332  */
1333 static int
1334 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1335 {
1336     int code;
1337     struct inode *ip = dp->d_inode;
1338
1339     AFS_GLOCK();
1340     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1341     AFS_GUNLOCK();
1342     return code;
1343 }
1344
1345
1346 /* afs_linux_follow_link
1347  * a file system dependent link following routine.
1348  */
1349 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1350 {
1351     int code;
1352     char *name;
1353
1354     name = osi_Alloc(PATH_MAX);
1355     if (!name) {
1356         return -EIO;
1357     }
1358
1359     AFS_GLOCK();
1360     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1361     AFS_GUNLOCK();
1362
1363     if (code < 0) {
1364         goto out;
1365     }
1366
1367     name[code] = '\0';
1368     code = vfs_follow_link(nd, name);
1369
1370 out:
1371     osi_Free(name, PATH_MAX);
1372
1373     return code;
1374 }
1375
1376 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1377
1378 #if defined(AFS_CACHE_BYPASS)
1379 #endif /* defined(AFS_CACHE_BYPASS */
1380
1381 /* Populate a page by filling it from the cache file pointed at by cachefp
1382  * (which contains indicated chunk)
1383  * If task is NULL, the page copy occurs syncronously, and the routine
1384  * returns with page still locked. If task is non-NULL, then page copies
1385  * may occur in the background, and the page will be unlocked when it is
1386  * ready for use.
1387  */
1388 static int
1389 afs_linux_read_cache(struct file *cachefp, struct page *page,
1390                      int chunk, struct pagevec *lrupv,
1391                      struct afs_pagecopy_task *task) {
1392     loff_t offset = page_offset(page);
1393     struct page *newpage, *cachepage;
1394     struct address_space *cachemapping;
1395     int pageindex;
1396     int code = 0;
1397
1398     cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1399     newpage = NULL;
1400     cachepage = NULL;
1401
1402     /* From our offset, we now need to work out which page in the disk
1403      * file it corresponds to. This will be fun ... */
1404     pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1405
1406     while (cachepage == NULL) {
1407         cachepage = find_get_page(cachemapping, pageindex);
1408         if (!cachepage) {
1409             if (!newpage)
1410                 newpage = page_cache_alloc_cold(cachemapping);
1411             if (!newpage) {
1412                 code = -ENOMEM;
1413                 goto out;
1414             }
1415
1416             code = add_to_page_cache(newpage, cachemapping,
1417                                      pageindex, GFP_KERNEL);
1418             if (code == 0) {
1419                 cachepage = newpage;
1420                 newpage = NULL;
1421
1422                 page_cache_get(cachepage);
1423                 if (!pagevec_add(lrupv, cachepage))
1424                     __pagevec_lru_add_file(lrupv);
1425
1426             } else {
1427                 page_cache_release(newpage);
1428                 newpage = NULL;
1429                 if (code != -EEXIST)
1430                     goto out;
1431             }
1432         } else {
1433             lock_page(cachepage);
1434         }
1435     }
1436
1437     if (!PageUptodate(cachepage)) {
1438         ClearPageError(cachepage);
1439         code = cachemapping->a_ops->readpage(NULL, cachepage);
1440         if (!code && !task) {
1441             wait_on_page_locked(cachepage);
1442         }
1443     } else {
1444         unlock_page(cachepage);
1445     }
1446
1447     if (!code) {
1448         if (PageUptodate(cachepage)) {
1449             copy_highpage(page, cachepage);
1450             flush_dcache_page(page);
1451             SetPageUptodate(page);
1452
1453             if (task)
1454                 unlock_page(page);
1455         } else if (task) {
1456             afs_pagecopy_queue_page(task, cachepage, page);
1457         } else {
1458             code = -EIO;
1459         }
1460     }
1461
1462     if (code && task) {
1463         unlock_page(page);
1464     }
1465
1466 out:
1467     if (cachepage)
1468         page_cache_release(cachepage);
1469
1470     return code;
1471 }
1472
1473 static int inline
1474 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1475 {
1476     loff_t offset = page_offset(pp);
1477     struct inode *ip = FILE_INODE(fp);
1478     struct vcache *avc = VTOAFS(ip);
1479     struct dcache *tdc;
1480     struct file *cacheFp = NULL;
1481     int code;
1482     int dcLocked = 0;
1483     struct pagevec lrupv;
1484
1485     /* Not a UFS cache, don't do anything */
1486     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1487         return 0;
1488
1489     /* Can't do anything if the vcache isn't statd , or if the read
1490      * crosses a chunk boundary.
1491      */
1492     if (!(avc->f.states & CStatd) ||
1493         AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1494         return 0;
1495     }
1496
1497     ObtainWriteLock(&avc->lock, 911);
1498
1499     /* XXX - See if hinting actually makes things faster !!! */
1500
1501     /* See if we have a suitable entry already cached */
1502     tdc = avc->dchint;
1503
1504     if (tdc) {
1505         /* We need to lock xdcache, then dcache, to handle situations where
1506          * the hint is on the free list. However, we can't safely do this
1507          * according to the locking hierarchy. So, use a non blocking lock.
1508          */
1509         ObtainReadLock(&afs_xdcache);
1510         dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1511
1512         if (dcLocked && (tdc->index != NULLIDX)
1513             && !FidCmp(&tdc->f.fid, &avc->f.fid)
1514             && tdc->f.chunk == AFS_CHUNK(offset)
1515             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1516             /* Bonus - the hint was correct */
1517             afs_RefDCache(tdc);
1518         } else {
1519             /* Only destroy the hint if its actually invalid, not if there's
1520              * just been a locking failure */
1521             if (dcLocked) {
1522                 ReleaseReadLock(&tdc->lock);
1523                 avc->dchint = NULL;
1524             }
1525
1526             tdc = NULL;
1527             dcLocked = 0;
1528         }
1529         ReleaseReadLock(&afs_xdcache);
1530     }
1531
1532     /* No hint, or hint is no longer valid - see if we can get something
1533      * directly from the dcache
1534      */
1535     if (!tdc)
1536         tdc = afs_FindDCache(avc, offset);
1537
1538     if (!tdc) {
1539         ReleaseWriteLock(&avc->lock);
1540         return 0;
1541     }
1542
1543     if (!dcLocked)
1544         ObtainReadLock(&tdc->lock);
1545
1546     /* Is the dcache we've been given currently up to date */
1547     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1548         (tdc->dflags & DFFetching)) {
1549         ReleaseWriteLock(&avc->lock);
1550         ReleaseReadLock(&tdc->lock);
1551         afs_PutDCache(tdc);
1552         return 0;
1553     }
1554
1555     /* Update our hint for future abuse */
1556     avc->dchint = tdc;
1557
1558     /* Okay, so we've now got a cache file that is up to date */
1559
1560     /* XXX - I suspect we should be locking the inodes before we use them! */
1561     AFS_GUNLOCK();
1562     cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1563     pagevec_init(&lrupv, 0);
1564
1565     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1566
1567     if (pagevec_count(&lrupv))
1568        __pagevec_lru_add_file(&lrupv);
1569
1570     filp_close(cacheFp, NULL);
1571     AFS_GLOCK();
1572
1573     ReleaseReadLock(&tdc->lock);
1574     ReleaseWriteLock(&avc->lock);
1575     afs_PutDCache(tdc);
1576
1577     *codep = code;
1578     return 1;
1579 }
1580
1581 /* afs_linux_readpage
1582  *
1583  * This function is split into two, because prepare_write/begin_write
1584  * require a readpage call which doesn't unlock the resulting page upon
1585  * success.
1586  */
1587 static int
1588 afs_linux_fillpage(struct file *fp, struct page *pp)
1589 {
1590     afs_int32 code;
1591     char *address;
1592     uio_t *auio;
1593     struct iovec *iovecp;
1594     struct inode *ip = FILE_INODE(fp);
1595     afs_int32 cnt = page_count(pp);
1596     struct vcache *avc = VTOAFS(ip);
1597     afs_offs_t offset = page_offset(pp);
1598     cred_t *credp;
1599
1600     AFS_GLOCK();
1601     if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1602         AFS_GUNLOCK();
1603         return code;
1604     }
1605     AFS_GUNLOCK();
1606
1607     credp = crref();
1608     address = kmap(pp);
1609     ClearPageError(pp);
1610
1611     auio = osi_Alloc(sizeof(uio_t));
1612     iovecp = osi_Alloc(sizeof(struct iovec));
1613
1614     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1615               AFS_UIOSYS);
1616
1617     afs_maybe_lock_kernel();
1618     AFS_GLOCK();
1619     AFS_DISCON_LOCK();
1620     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1621                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1622                99999);  /* not a possible code value */
1623
1624     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1625         
1626     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1627                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1628                code);
1629     AFS_DISCON_UNLOCK();
1630     AFS_GUNLOCK();
1631     afs_maybe_unlock_kernel();
1632     if (!code) {
1633         /* XXX valid for no-cache also?  Check last bits of files... :)
1634          * Cognate code goes in afs_NoCacheFetchProc.  */
1635         if (auio->uio_resid)    /* zero remainder of page */
1636              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1637                     auio->uio_resid);
1638
1639         flush_dcache_page(pp);
1640         SetPageUptodate(pp);
1641     } /* !code */
1642
1643     kunmap(pp);
1644
1645     osi_Free(auio, sizeof(uio_t));
1646     osi_Free(iovecp, sizeof(struct iovec));
1647
1648     crfree(credp);
1649     return afs_convert_code(code);
1650 }
1651
1652 static int
1653 afs_linux_prefetch(struct file *fp, struct page *pp)
1654 {
1655     int code = 0;
1656     struct vcache *avc = VTOAFS(FILE_INODE(fp));
1657     afs_offs_t offset = page_offset(pp);
1658
1659     if (AFS_CHUNKOFFSET(offset) == 0) {
1660         struct dcache *tdc;
1661         struct vrequest treq;
1662         cred_t *credp;
1663
1664         credp = crref();
1665         AFS_GLOCK();
1666         code = afs_InitReq(&treq, credp);
1667         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1668             tdc = afs_FindDCache(avc, offset);
1669             if (tdc) {
1670                 if (!(tdc->mflags & DFNextStarted))
1671                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1672                     afs_PutDCache(tdc);
1673             }
1674             ReleaseWriteLock(&avc->lock);
1675         }
1676         AFS_GUNLOCK();
1677         crfree(credp);
1678     }
1679     return afs_convert_code(code);
1680
1681 }
1682
1683 #if defined(AFS_CACHE_BYPASS)
1684
1685 static int
1686 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1687                            struct list_head *page_list, unsigned num_pages)
1688 {
1689     afs_int32 page_ix;
1690     uio_t *auio;
1691     afs_offs_t offset;
1692     struct iovec* iovecp;
1693     struct nocache_read_request *ancr;
1694     struct page *pp, *ppt;
1695     struct pagevec lrupv;
1696     afs_int32 code = 0;
1697
1698     cred_t *credp;
1699     struct inode *ip = FILE_INODE(fp);
1700     struct vcache *avc = VTOAFS(ip);
1701     afs_int32 base_index = 0;
1702     afs_int32 page_count = 0;
1703     afs_int32 isize;
1704
1705     /* background thread must free: iovecp, auio, ancr */
1706     iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1707
1708     auio = osi_Alloc(sizeof(uio_t));
1709     auio->uio_iov = iovecp;
1710     auio->uio_iovcnt = num_pages;
1711     auio->uio_flag = UIO_READ;
1712     auio->uio_seg = AFS_UIOSYS;
1713     auio->uio_resid = num_pages * PAGE_SIZE;
1714
1715     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1716     ancr->auio = auio;
1717     ancr->offset = auio->uio_offset;
1718     ancr->length = auio->uio_resid;
1719
1720     pagevec_init(&lrupv, 0);
1721
1722     for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1723
1724         if(list_empty(page_list))
1725             break;
1726
1727         pp = list_entry(page_list->prev, struct page, lru);
1728         /* If we allocate a page and don't remove it from page_list,
1729          * the page cache gets upset. */
1730         list_del(&pp->lru);
1731         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1732         if(pp->index > isize) {
1733             if(PageLocked(pp))
1734                 unlock_page(pp);
1735             continue;
1736         }
1737
1738         if(page_ix == 0) {
1739             offset = page_offset(pp);
1740             auio->uio_offset = offset;
1741             base_index = pp->index;
1742         }
1743         iovecp[page_ix].iov_len = PAGE_SIZE;
1744         code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1745         if(base_index != pp->index) {
1746             if(PageLocked(pp))
1747                  unlock_page(pp);
1748             page_cache_release(pp);
1749             iovecp[page_ix].iov_base = (void *) 0;
1750             base_index++;
1751             continue;
1752         }
1753         base_index++;
1754         if(code) {
1755             if(PageLocked(pp))
1756                 unlock_page(pp);
1757             page_cache_release(pp);
1758             iovecp[page_ix].iov_base = (void *) 0;
1759         } else {
1760             page_count++;
1761             if(!PageLocked(pp)) {
1762                 lock_page(pp);
1763             }
1764
1765             /* save the page for background map */
1766             iovecp[page_ix].iov_base = (void*) pp;
1767
1768             /* and put it on the LRU cache */
1769             if (!pagevec_add(&lrupv, pp))
1770                 __pagevec_lru_add(&lrupv);
1771         }
1772     }
1773
1774     /* If there were useful pages in the page list, make sure all pages
1775      * are in the LRU cache, then schedule the read */
1776     if(page_count) {
1777         pagevec_lru_add(&lrupv);
1778         credp = crref();
1779         code = afs_ReadNoCache(avc, ancr, credp);
1780         crfree(credp);
1781     } else {
1782         /* If there is nothing for the background thread to handle,
1783          * it won't be freeing the things that we never gave it */
1784         osi_Free(iovecp, num_pages * sizeof(struct iovec));
1785         osi_Free(auio, sizeof(uio_t));
1786         osi_Free(ancr, sizeof(struct nocache_read_request));
1787     }
1788     /* we do not flush, release, or unmap pages--that will be
1789      * done for us by the background thread as each page comes in
1790      * from the fileserver */
1791 out:
1792     return afs_convert_code(code);
1793 }
1794
1795
1796 static int
1797 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1798 {
1799     cred_t *credp = NULL;
1800     uio_t *auio;
1801     struct iovec *iovecp;
1802     struct nocache_read_request *ancr;
1803     afs_int32 isize;
1804
1805     ClearPageError(pp);
1806
1807     /* receiver frees */
1808     auio = osi_Alloc(sizeof(uio_t));
1809     iovecp = osi_Alloc(sizeof(struct iovec));
1810
1811     /* address can be NULL, because we overwrite it with 'pp', below */
1812     setup_uio(auio, iovecp, NULL, page_offset(pp),
1813               PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1814
1815     /* save the page for background map */
1816     /* XXX - Shouldn't we get a reference count here? */
1817     auio->uio_iov->iov_base = (void*) pp;
1818     /* the background thread will free this */
1819     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1820     ancr->auio = auio;
1821     ancr->offset = offset;
1822     ancr->length = PAGE_SIZE;
1823
1824     credp = crref();
1825     afs_maybe_lock_kernel();
1826     code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1827     afs_maybe_unlock_kernel();
1828     crfree(credp);
1829
1830     return afs_convert_code(code);
1831 }
1832
1833 static inline int
1834 afs_linux_can_bypass(struct inode *ip) {
1835     switch(cache_bypass_strategy) {
1836         case NEVER_BYPASS_CACHE:
1837             return 0;
1838         case ALWAYS_BYPASS_CACHE:
1839             return 1;
1840         case LARGE_FILES_BYPASS_CACHE:
1841             if(i_size_read(ip) > cache_bypass_threshold)
1842                 return 1;
1843         default:
1844      }
1845      return 0;
1846 }
1847
1848 /* Check if a file is permitted to bypass the cache by policy, and modify
1849  * the cache bypass state recorded for that file */
1850
1851 static inline int
1852 afs_linux_bypass_check(struct inode *ip) {
1853     struct cred* credp;
1854
1855     int bypass = afs_linux_can_bypass(ip);
1856
1857     credp = crref();
1858     trydo_cache_transition(VTOAFS(ip)), credp, bypass);
1859     crfree(credp);
1860
1861     return bypass;
1862 }
1863
1864 #else
1865 static inline int
1866 afs_linux_bypass_check(struct inode *ip) {
1867     return 0;
1868 }
1869 static inline int
1870 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1871     return 0;
1872 }
1873 static inline int
1874 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1875                     struct list_head *page_list, unsigned int num_pages) {
1876     return 0;
1877 }
1878 #endif
1879
1880 static int
1881 afs_linux_readpage(struct file *fp, struct page *pp)
1882 {
1883     int code;
1884
1885     if (afs_linux_bypass_check(FILE_INODE(fp))) {
1886         code = afs_linux_bypass_readpage(fp, pp);
1887     } else {
1888         code = afs_linux_fillpage(fp, pp);
1889         if (!code)
1890             code = afs_linux_prefetch(fp, pp);
1891         unlock_page(pp);
1892     }
1893
1894     return code;
1895 }
1896
1897 /* Readpages reads a number of pages for a particular file. We use
1898  * this to optimise the reading, by limiting the number of times upon which
1899  * we have to lookup, lock and open vcaches and dcaches
1900  */
1901
1902 static int
1903 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1904                     struct list_head *page_list, unsigned int num_pages)
1905 {
1906     struct inode *inode = mapping->host;
1907     struct vcache *avc = VTOAFS(inode);
1908     struct dcache *tdc;
1909     struct file *cacheFp = NULL;
1910     int code;
1911     unsigned int page_idx;
1912     loff_t offset;
1913     struct pagevec lrupv;
1914     struct afs_pagecopy_task *task;
1915
1916     if (afs_linux_bypass_check(inode))
1917         return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1918
1919     AFS_GLOCK();
1920     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1921         AFS_GUNLOCK();
1922         return code;
1923     }
1924
1925     ObtainWriteLock(&avc->lock, 912);
1926     AFS_GUNLOCK();
1927
1928     task = afs_pagecopy_init_task();
1929
1930     tdc = NULL;
1931     pagevec_init(&lrupv, 0);
1932     for (page_idx = 0; page_idx < num_pages; page_idx++) {
1933         struct page *page = list_entry(page_list->prev, struct page, lru);
1934         list_del(&page->lru);
1935         offset = page_offset(page);
1936
1937         if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1938             AFS_GLOCK();
1939             ReleaseReadLock(&tdc->lock);
1940             afs_PutDCache(tdc);
1941             AFS_GUNLOCK();
1942             tdc = NULL;
1943             if (cacheFp)
1944                 filp_close(cacheFp, NULL);
1945         }
1946
1947         if (!tdc) {
1948             AFS_GLOCK();
1949             if ((tdc = afs_FindDCache(avc, offset))) {
1950                 ObtainReadLock(&tdc->lock);
1951                 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1952                     (tdc->dflags & DFFetching)) {
1953                     ReleaseReadLock(&tdc->lock);
1954                     afs_PutDCache(tdc);
1955                     tdc = NULL;
1956                 }
1957             }
1958             AFS_GUNLOCK();
1959             if (tdc)
1960                 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1961         }
1962
1963         if (tdc && !add_to_page_cache(page, mapping, page->index,
1964                                       GFP_KERNEL)) {
1965             page_cache_get(page);
1966             if (!pagevec_add(&lrupv, page))
1967                 __pagevec_lru_add_file(&lrupv);
1968
1969             afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
1970         }
1971         page_cache_release(page);
1972     }
1973     if (pagevec_count(&lrupv))
1974        __pagevec_lru_add_file(&lrupv);
1975
1976     if (tdc)
1977         filp_close(cacheFp, NULL);
1978
1979     afs_pagecopy_put_task(task);
1980
1981     AFS_GLOCK();
1982     if (tdc) {
1983         ReleaseReadLock(&tdc->lock);
1984         afs_PutDCache(tdc);
1985     }
1986
1987     ReleaseWriteLock(&avc->lock);
1988     AFS_GUNLOCK();
1989     return 0;
1990 }
1991
1992 /* Prepare an AFS vcache for writeback. Should be called with the vcache
1993  * locked */
1994 static inline int
1995 afs_linux_prepare_writeback(struct vcache *avc) {
1996     if (avc->f.states & CPageWrite) {
1997         return AOP_WRITEPAGE_ACTIVATE;
1998     }
1999     avc->f.states |= CPageWrite;
2000     return 0;
2001 }
2002
2003 static inline int
2004 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2005     struct vrequest treq;
2006     int code = 0;
2007
2008     if (!afs_InitReq(&treq, credp))
2009         code = afs_DoPartialWrite(avc, &treq);
2010
2011     return afs_convert_code(code);
2012 }
2013
2014 static inline void
2015 afs_linux_complete_writeback(struct vcache *avc) {
2016     avc->f.states &= ~CPageWrite;
2017 }
2018
2019 /* Writeback a given page syncronously. Called with no AFS locks held */
2020 static int
2021 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2022                          unsigned long offset, unsigned int count,
2023                          cred_t *credp)
2024 {
2025     struct vcache *vcp = VTOAFS(ip);
2026     char *buffer;
2027     afs_offs_t base;
2028     int code = 0;
2029     uio_t tuio;
2030     struct iovec iovec;
2031     int f_flags = 0;
2032
2033     buffer = kmap(pp) + offset;
2034     base = page_offset(pp) + offset;
2035
2036     afs_maybe_lock_kernel();
2037     AFS_GLOCK();
2038     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2039                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2040                ICL_TYPE_INT32, 99999);
2041
2042     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2043
2044     code = afs_write(vcp, &tuio, f_flags, credp, 0);
2045
2046     i_size_write(ip, vcp->f.m.Length);
2047     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2048
2049     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2050
2051     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2052                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2053                ICL_TYPE_INT32, code);
2054
2055     AFS_GUNLOCK();
2056     afs_maybe_unlock_kernel();
2057     kunmap(pp);
2058
2059     return code;
2060 }
2061
2062 static int
2063 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2064                          unsigned long offset, unsigned int count)
2065 {
2066     int code;
2067     int code1 = 0;
2068     struct vcache *vcp = VTOAFS(ip);
2069     cred_t *credp;
2070
2071     /* Catch recursive writeback. This occurs if the kernel decides
2072      * writeback is required whilst we are writing to the cache, or
2073      * flushing to the server. When we're running syncronously (as
2074      * opposed to from writepage) we can't actually do anything about
2075      * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2076      */
2077     AFS_GLOCK();
2078     ObtainWriteLock(&vcp->lock, 532);
2079     afs_linux_prepare_writeback(vcp);
2080     ReleaseWriteLock(&vcp->lock);
2081     AFS_GUNLOCK();
2082
2083     credp = crref();
2084     code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2085
2086     afs_maybe_lock_kernel();
2087     AFS_GLOCK();
2088     ObtainWriteLock(&vcp->lock, 533);
2089     if (code > 0)
2090         code1 = afs_linux_dopartialwrite(vcp, credp);
2091     afs_linux_complete_writeback(vcp);
2092     ReleaseWriteLock(&vcp->lock);
2093     AFS_GUNLOCK();
2094     afs_maybe_unlock_kernel();
2095     crfree(credp);
2096
2097     if (code1)
2098         return code1;
2099
2100     return code;
2101 }
2102
2103 static int
2104 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2105 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2106 #else
2107 afs_linux_writepage(struct page *pp)
2108 #endif
2109 {
2110     struct address_space *mapping = pp->mapping;
2111     struct inode *inode;
2112     struct vcache *vcp;
2113     cred_t *credp;
2114     unsigned int to = PAGE_CACHE_SIZE;
2115     loff_t isize;
2116     int code = 0;
2117     int code1 = 0;
2118
2119     if (PageReclaim(pp)) {
2120         return AOP_WRITEPAGE_ACTIVATE;
2121         /* XXX - Do we need to redirty the page here? */
2122     }
2123
2124     page_cache_get(pp);
2125
2126     inode = mapping->host;
2127     vcp = VTOAFS(inode);
2128     isize = i_size_read(inode);
2129
2130     /* Don't defeat an earlier truncate */
2131     if (page_offset(pp) > isize)
2132         goto done;
2133
2134     AFS_GLOCK();
2135     ObtainWriteLock(&vcp->lock, 537);
2136     code = afs_linux_prepare_writeback(vcp);
2137     if (code) {
2138         ReleaseWriteLock(&vcp->lock);
2139         AFS_GUNLOCK();
2140         return code;
2141     }
2142     /* Grab the creds structure currently held in the vnode, and
2143      * get a reference to it, in case it goes away ... */
2144     credp = vcp->cred;
2145     crhold(credp);
2146     ReleaseWriteLock(&vcp->lock);
2147     AFS_GUNLOCK();
2148
2149     /* If this is the final page, then just write the number of bytes that
2150      * are actually in it */
2151     if ((isize - page_offset(pp)) < to )
2152         to = isize - page_offset(pp);
2153
2154     code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2155
2156     afs_maybe_lock_kernel();
2157     AFS_GLOCK();
2158     ObtainWriteLock(&vcp->lock, 538);
2159
2160     /* As much as we might like to ignore a file server error here,
2161      * and just try again when we close(), unfortunately StoreAllSegments
2162      * will invalidate our chunks if the server returns a permanent error,
2163      * so we need to at least try and get that error back to the user
2164      */
2165     if (code == to)
2166         code1 = afs_linux_dopartialwrite(vcp, credp);
2167
2168     afs_linux_complete_writeback(vcp);
2169     ReleaseWriteLock(&vcp->lock);
2170     crfree(credp);
2171     AFS_GUNLOCK();
2172     afs_maybe_unlock_kernel();
2173
2174 done:
2175     SetPageUptodate(pp);
2176     if ( code != AOP_WRITEPAGE_ACTIVATE ) {
2177         /* XXX - do we need to redirty the page here? */
2178         unlock_page(pp);
2179     }
2180
2181     page_cache_release(pp);
2182
2183     if (code1)
2184         return code1;
2185
2186     if (code == to)
2187         return 0;
2188
2189     return code;
2190 }
2191
2192 /* afs_linux_permission
2193  * Check access rights - returns error if can't check or permission denied.
2194  */
2195 static int
2196 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2197 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2198 #else
2199 afs_linux_permission(struct inode *ip, int mode)
2200 #endif
2201 {
2202     int code;
2203     cred_t *credp = crref();
2204     int tmp = 0;
2205
2206     AFS_GLOCK();
2207     if (mode & MAY_EXEC)
2208         tmp |= VEXEC;
2209     if (mode & MAY_READ)
2210         tmp |= VREAD;
2211     if (mode & MAY_WRITE)
2212         tmp |= VWRITE;
2213     code = afs_access(VTOAFS(ip), tmp, credp);
2214
2215     AFS_GUNLOCK();
2216     crfree(credp);
2217     return afs_convert_code(code);
2218 }
2219
2220 #if !defined(HAVE_WRITE_BEGIN)
2221 static int
2222 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2223                        unsigned to)
2224 {
2225     int code;
2226
2227     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2228                                     offset, to - offset);
2229
2230     return code;
2231 }
2232
2233 static int
2234 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2235                         unsigned to)
2236 {
2237     return 0;
2238 }
2239 #else
2240
2241 static int
2242 afs_linux_write_end(struct file *file, struct address_space *mapping,
2243                                 loff_t pos, unsigned len, unsigned copied,
2244                                 struct page *page, void *fsdata)
2245 {
2246     int code;
2247     unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2248
2249     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2250                                     from, copied);
2251     unlock_page(page);
2252     page_cache_release(page);
2253     return code;
2254 }
2255
2256 static int
2257 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2258                                 loff_t pos, unsigned len, unsigned flags,
2259                                 struct page **pagep, void **fsdata)
2260 {
2261     struct page *page;
2262     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2263     page = grab_cache_page_write_begin(mapping, index, flags);
2264     *pagep = page;
2265
2266     return 0;
2267 }
2268 #endif
2269
2270
2271 static struct inode_operations afs_file_iops = {
2272   .permission =         afs_linux_permission,
2273   .getattr =            afs_linux_getattr,
2274   .setattr =            afs_notify_change,
2275 };
2276
2277 static struct address_space_operations afs_file_aops = {
2278   .readpage =           afs_linux_readpage,
2279   .readpages =          afs_linux_readpages,
2280   .writepage =          afs_linux_writepage,
2281 #if defined (HAVE_WRITE_BEGIN)
2282   .write_begin =        afs_linux_write_begin,
2283   .write_end =          afs_linux_write_end,
2284 #else
2285   .commit_write =       afs_linux_commit_write,
2286   .prepare_write =      afs_linux_prepare_write,
2287 #endif
2288 };
2289
2290
2291 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2292  * by what sort of operation is allowed.....
2293  */
2294
2295 static struct inode_operations afs_dir_iops = {
2296   .setattr =            afs_notify_change,
2297   .create =             afs_linux_create,
2298   .lookup =             afs_linux_lookup,
2299   .link =               afs_linux_link,
2300   .unlink =             afs_linux_unlink,
2301   .symlink =            afs_linux_symlink,
2302   .mkdir =              afs_linux_mkdir,
2303   .rmdir =              afs_linux_rmdir,
2304   .rename =             afs_linux_rename,
2305   .getattr =            afs_linux_getattr,
2306   .permission =         afs_linux_permission,
2307 };
2308
2309 /* We really need a separate symlink set of ops, since do_follow_link()
2310  * determines if it _is_ a link by checking if the follow_link op is set.
2311  */
2312 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2313 static int
2314 afs_symlink_filler(struct file *file, struct page *page)
2315 {
2316     struct inode *ip = (struct inode *)page->mapping->host;
2317     char *p = (char *)kmap(page);
2318     int code;
2319
2320     afs_maybe_lock_kernel();
2321     AFS_GLOCK();
2322     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2323     AFS_GUNLOCK();
2324
2325     if (code < 0)
2326         goto fail;
2327     p[code] = '\0';             /* null terminate? */
2328     afs_maybe_unlock_kernel();
2329
2330     SetPageUptodate(page);
2331     kunmap(page);
2332     unlock_page(page);
2333     return 0;
2334
2335   fail:
2336     afs_maybe_unlock_kernel();
2337
2338     SetPageError(page);
2339     kunmap(page);
2340     unlock_page(page);
2341     return code;
2342 }
2343
2344 static struct address_space_operations afs_symlink_aops = {
2345   .readpage =   afs_symlink_filler
2346 };
2347 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2348
2349 static struct inode_operations afs_symlink_iops = {
2350 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2351   .readlink =           page_readlink,
2352 # if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2353   .follow_link =        page_follow_link,
2354 # else
2355   .follow_link =        page_follow_link_light,
2356   .put_link =           page_put_link,
2357 # endif
2358 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2359   .readlink =           afs_linux_readlink,
2360   .follow_link =        afs_linux_follow_link,
2361 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2362   .setattr =            afs_notify_change,
2363 };
2364
2365 void
2366 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2367 {
2368         
2369     if (vattr)
2370         vattr2inode(ip, vattr);
2371
2372     ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2373 /* Reset ops if symlink or directory. */
2374     if (S_ISREG(ip->i_mode)) {
2375         ip->i_op = &afs_file_iops;
2376         ip->i_fop = &afs_file_fops;
2377         ip->i_data.a_ops = &afs_file_aops;
2378
2379     } else if (S_ISDIR(ip->i_mode)) {
2380         ip->i_op = &afs_dir_iops;
2381         ip->i_fop = &afs_dir_fops;
2382
2383     } else if (S_ISLNK(ip->i_mode)) {
2384         ip->i_op = &afs_symlink_iops;
2385 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2386         ip->i_data.a_ops = &afs_symlink_aops;
2387         ip->i_mapping = &ip->i_data;
2388 #endif
2389     }
2390
2391 }