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