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