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