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