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