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